C# 将位图旋转90度

发布于 2024-08-20 16:17:49 字数 607 浏览 3 评论 0原文

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

请注意 returnBitmap width = original.height 和 height = original.width

任何人都可以帮助我解决这个问题或指出我做错了什么吗?

    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;
    }

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.

Notice the returnBitmap width = original.height and it's 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;
    }

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

梦里寻她 2024-08-27 16:17:49

这个怎么样:

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);
    }
}

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);
    }
}
请帮我爱他 2024-08-27 16:17:49

该错误出现在您第一次调用 TranslateTransform 中:

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

此转换需要位于 returnBitmap 的坐标空间中,而不是 b 中,因此应该是:

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

或等效地

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

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

不过,正如 Rubens Farias 建议的那样,您可能最好使用更简单的 RotateFlip 方法。

The bug is in your first call to TranslateTransform:

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

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

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

or equivalently

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

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

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

冷情妓 2024-08-27 16:17:49

我遇到了,经过一点修改,我就让它工作了。我发现了一些其他的例子,并注意到缺少一些对我来说很重要的东西。我必须调用 SetResolution,如果不调用,图像最终的尺寸就会错误。我还注意到高度和宽度向后,尽管我认为无论如何对于非方形图像都会有一些修改。我想我会把这篇文章发布给任何遇到这个问题的人,就像我遇到同样的问题一样。

这是我的代码

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);
    }
}

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.

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);
    }
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文