Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/image-processing/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 正确旋转图像_C#_Image Processing - Fatal编程技术网

C# 正确旋转图像

C# 正确旋转图像,c#,image-processing,C#,Image Processing,如何旋转图像而不显示为这样 以下是我的旋转方法: public static Bitmap RotateImageN(Bitmap bmp, float angle) { Bitmap rotatedImage = new Bitmap(bmp.Width, bmp.Height); using (Graphics g = Graphics.FromImage(rotatedImage)) { // Set the

如何旋转图像而不显示为这样

以下是我的旋转方法:

public static Bitmap RotateImageN(Bitmap bmp, float angle)
    {
        Bitmap rotatedImage = new Bitmap(bmp.Width, bmp.Height);
        using (Graphics g = Graphics.FromImage(rotatedImage))
        {
            // Set the rotation point to the center in the matrix
            g.TranslateTransform(bmp.Width / 2, bmp.Height / 2);
            // Rotate
            g.RotateTransform(angle);
            // Restore rotation point in the matrix
            g.TranslateTransform(-bmp.Width / 2, -bmp.Height / 2);
            // Draw the image on the bitmap
            g.DrawImage(bmp, new Point(0, 0));
        }

        return rotatedImage;
    }
编辑:尝试Loocid的代码后


您的
旋转图像
位图需要足够大以容纳旋转图像

假设将原始图像旋转30°,则需要获得边界框的大小,如下所示:

使用一些基本触发器:

x=L*cos(30*π/180)+w*cos(60*π/180)

y=L*sin(30*π/180)+w*sin(60*π/180)

因此,将代码的开头更改为:

var x = bmp.Width * Math.Cos(angle * Math.PI / 180) + bmp.Height * Math.Cos((90-angle) * Math.PI / 180)
var y = bmp.Width * Math.Sin(angle * Math.PI / 180) + bmp.Height * Math.Sin((90-angle) * Math.PI / 180)
Bitmap rotatedImage = new Bitmap(x, y);

您的
rotateImage
位图需要足够大才能容纳旋转后的图像

假设将原始图像旋转30°,则需要获得边界框的大小,如下所示:

使用一些基本触发器:

x=L*cos(30*π/180)+w*cos(60*π/180)

y=L*sin(30*π/180)+w*sin(60*π/180)

因此,将代码的开头更改为:

var x = bmp.Width * Math.Cos(angle * Math.PI / 180) + bmp.Height * Math.Cos((90-angle) * Math.PI / 180)
var y = bmp.Width * Math.Sin(angle * Math.PI / 180) + bmp.Height * Math.Sin((90-angle) * Math.PI / 180)
Bitmap rotatedImage = new Bitmap(x, y);

旋转中出现的问题与边界框有关。由于您提供的图像与您提供的区域不匹配,因此正在剪裁边缘

我也面临这个问题。所以我尝试了一个解决方案

添加适合我的代码

public static Bitmap RotateImageN(Bitmap bitmap, float angle)
{
    Matrix matrix = new Matrix();
    matrix.Translate(bitmap.Width / -2, bitmap.Height / -2, MatrixOrder.Append);
    matrix.RotateAt(angle, new System.Drawing.Point(0, 0), MatrixOrder.Append);
    using (GraphicsPath graphicsPath = new GraphicsPath())
    {
        graphicsPath.AddPolygon(new System.Drawing.Point[] { new System.Drawing.Point(0, 0), new System.Drawing.Point(bitmap.Width, 0), new System.Drawing.Point(0, bitmap.Height) });
        graphicsPath.Transform(matrix);
        System.Drawing.PointF[] points = graphicsPath.PathPoints;

        Rectangle rectangle = boundingBox(bitmap, matrix);
        Bitmap resultBitmap = new Bitmap(rectangle.Width, rectangle.Height);

        using (Graphics gDest = Graphics.FromImage(resultBitmap))
        {
            Matrix mDest = new Matrix();
            mDest.Translate(resultBitmap.Width / 2, resultBitmap.Height / 2, MatrixOrder.Append);
            gDest.Transform = mDest;
            gDest.DrawImage(bitmap, points);
            return resultBitmap;
        }
    }
}

private static Rectangle boundingBox(Image image, Matrix matrix)
{
    GraphicsUnit graphicsUnit = new GraphicsUnit();
    Rectangle boundingRectangle = Rectangle.Round(image.GetBounds(ref graphicsUnit));
    Point topLeft = new Point(boundingRectangle.Left, boundingRectangle.Top);
    Point topRight = new Point(boundingRectangle.Right, boundingRectangle.Top);
    Point bottomRight = new Point(boundingRectangle.Right, boundingRectangle.Bottom);
    Point bottomLeft = new Point(boundingRectangle.Left, boundingRectangle.Bottom);
    Point[] points = new Point[] { topLeft, topRight, bottomRight, bottomLeft };
    GraphicsPath graphicsPath = new GraphicsPath(points, new byte[] { (byte)PathPointType.Start, (byte)PathPointType.Line, (byte)PathPointType.Line, (byte)PathPointType.Line });
    graphicsPath.Transform(matrix);
    return Rectangle.Round(graphicsPath.GetBounds());
}

旋转中出现的问题与边界框有关。由于您提供的图像与您提供的区域不匹配,因此正在剪裁边缘

我也面临这个问题。所以我尝试了一个解决方案

添加适合我的代码

public static Bitmap RotateImageN(Bitmap bitmap, float angle)
{
    Matrix matrix = new Matrix();
    matrix.Translate(bitmap.Width / -2, bitmap.Height / -2, MatrixOrder.Append);
    matrix.RotateAt(angle, new System.Drawing.Point(0, 0), MatrixOrder.Append);
    using (GraphicsPath graphicsPath = new GraphicsPath())
    {
        graphicsPath.AddPolygon(new System.Drawing.Point[] { new System.Drawing.Point(0, 0), new System.Drawing.Point(bitmap.Width, 0), new System.Drawing.Point(0, bitmap.Height) });
        graphicsPath.Transform(matrix);
        System.Drawing.PointF[] points = graphicsPath.PathPoints;

        Rectangle rectangle = boundingBox(bitmap, matrix);
        Bitmap resultBitmap = new Bitmap(rectangle.Width, rectangle.Height);

        using (Graphics gDest = Graphics.FromImage(resultBitmap))
        {
            Matrix mDest = new Matrix();
            mDest.Translate(resultBitmap.Width / 2, resultBitmap.Height / 2, MatrixOrder.Append);
            gDest.Transform = mDest;
            gDest.DrawImage(bitmap, points);
            return resultBitmap;
        }
    }
}

private static Rectangle boundingBox(Image image, Matrix matrix)
{
    GraphicsUnit graphicsUnit = new GraphicsUnit();
    Rectangle boundingRectangle = Rectangle.Round(image.GetBounds(ref graphicsUnit));
    Point topLeft = new Point(boundingRectangle.Left, boundingRectangle.Top);
    Point topRight = new Point(boundingRectangle.Right, boundingRectangle.Top);
    Point bottomRight = new Point(boundingRectangle.Right, boundingRectangle.Bottom);
    Point bottomLeft = new Point(boundingRectangle.Left, boundingRectangle.Bottom);
    Point[] points = new Point[] { topLeft, topRight, bottomRight, bottomLeft };
    GraphicsPath graphicsPath = new GraphicsPath(points, new byte[] { (byte)PathPointType.Start, (byte)PathPointType.Line, (byte)PathPointType.Line, (byte)PathPointType.Line });
    graphicsPath.Transform(matrix);
    return Rectangle.Round(graphicsPath.GetBounds());
}


搜索边界矩形需要较大的结果位图或缩小源。搜索边界矩形需要较大的结果位图或缩小源。位图旋转图像=新位图(x,y);我猜基本上是这样写的。C#有
Math.Sin
Math.Cos
Math.PI
。如果我在绘图图像中使用它作为我的x和y,它就不会绘图。你是否将30和60更改为
angle
90-angle
?@无目标我已更新我的答案,以包含你需要使用的C#代码。这是假设角度以度为单位。位图旋转图像=新位图(x,y);我猜基本上是这样写的。C#有
Math.Sin
Math.Cos
Math.PI
。如果我在绘图图像中使用它作为我的x和y,它就不会绘图。你是否将30和60更改为
angle
90-angle
?@无目标我已更新我的答案,以包含你需要使用的C#代码。这是假设
角度
以度为单位。它现在不裁剪,但也不能完全旋转。请显示原始图像并告诉我旋转角度。原始图像:。旋转角度:52度这对我来说很好。我想问题可能在别的地方。在单独的exe中测试图像旋转,以便您了解情况。如何在picturebox中绘制图像?它现在不会剪切,但也不会完全旋转。请显示原始图像并告诉我旋转角度。原始图像:。旋转角度:52度这对我来说很好。我想问题可能在别的地方。在单独的exe中测试图像旋转,以便您了解情况。如何在picturebox中绘制图像?