C# 旋转位图 90 度

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/2225363/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-07 00:32:14  来源:igfitidea点击:

C# rotate bitmap 90 degrees

c#bitmap

提问by Kevin

I'm trying to rotate a bitmap 90 degrees using the following function. The problem with it is that it cuts off part of the image when the height and width are not equal.

我正在尝试使用以下函数将位图旋转 90 度。它的问题在于,当高度和宽度不相等时,它会切断图像的一部分。

Notice the returnBitmap width = original.height and it's height = original.width

注意 returnBitmap width = original.height 和它的 height = original.width

Can anyone help me solve this issue or point out what I'm doing wrong?

谁能帮我解决这个问题或指出我做错了什么?

    private Bitmap rotateImage90(Bitmap b)
    {
        Bitmap returnBitmap = new Bitmap(b.Height, b.Width);
        Graphics g = Graphics.FromImage(returnBitmap);
        g.TranslateTransform((float)b.Width / 2, (float)b.Height / 2);
        g.RotateTransform(90);
        g.TranslateTransform(-(float)b.Width / 2, -(float)b.Height / 2);
        g.DrawImage(b, new Point(0, 0));
        return returnBitmap;
    }

采纳答案by Rubens Farias

What about this:

这个怎么样:

private void RotateAndSaveImage(String input, String output)
{
    //create an object that we can use to examine an image file
    using (Image img = Image.FromFile(input))
    {
        //rotate the picture by 90 degrees and re-save the picture as a Jpeg
        img.RotateFlip(RotateFlipType.Rotate90FlipNone);
        img.Save(output, System.Drawing.Imaging.ImageFormat.Jpeg);
    }
}

回答by finnw

The bug is in your first call to TranslateTransform:

该错误出现在您第一次致电TranslateTransform

g.TranslateTransform((float)b.Width / 2, (float)b.Height / 2);

This transform needs to be in the coordinate space of returnBitmaprather than b, so this should be:

这个变换需要在returnBitmap而不是的坐标空间中b,所以这应该是:

g.TranslateTransform((float)b.Height / 2, (float)b.Width / 2);

or equivalently

或等效地

g.TranslateTransform((float)returnBitmap.Width / 2, (float)returnBitmap.Height / 2);

Your second TranslateTransformis correct, because it will be applied before the rotation.

你的第二个TranslateTransform是正确的,因为它将在旋转之前应用。

However you're probably better off with the simpler RotateFlipmethod, as Rubens Farias suggested.

但是RotateFlip,正如 Rubens Farias 建议的那样,使用更简单的方法可能会更好。

回答by thecaptain0220

I came across and with a little modification I got it to work. I found some other examples and noticed something missing that made the difference for me. I had to call SetResolution, if I didn't the image ended up the wrong size. I also noticed the Height and Width were backwards, although I think there would be some modification for a non square image anyway. I figured I would post this for anyone who comes across this like I did with the same problem.

我遇到了一些修改,我让它工作。我找到了一些其他示例,并注意到缺少一些对我产生影响的东西。我不得不调用 SetResolution,如果我不这样做,图像最终会变成错误的大小。我还注意到高度和宽度是向后的,尽管我认为无论如何都会对非方形图像进行一些修改。我想我会为任何遇到这个问题的人发布这个,就像我遇到同样的问题一样。

Here is my code

这是我的代码

private static void RotateAndSaveImage(string input, string output, int angle)
{
    //Open the source image and create the bitmap for the rotatated image
    using (Bitmap sourceImage = new Bitmap(input))
    using (Bitmap rotateImage = new Bitmap(sourceImage.Width, sourceImage.Height))
    {
        //Set the resolution for the rotation image
        rotateImage.SetResolution(sourceImage.HorizontalResolution, sourceImage.VerticalResolution);
        //Create a graphics object
        using (Graphics gdi = Graphics.FromImage(rotateImage))
        {
            //Rotate the image
            gdi.TranslateTransform((float)sourceImage.Width / 2, (float)sourceImage.Height / 2);
            gdi.RotateTransform(angle);
            gdi.TranslateTransform(-(float)sourceImage.Width / 2, -(float)sourceImage.Height / 2);
            gdi.DrawImage(sourceImage, new System.Drawing.Point(0, 0));
        }

        //Save to a file
        rotateImage.Save(output);
    }
}