C#位图旋转90度 [英] C# rotate bitmap 90 degrees

查看:606
本文介绍了C#位图旋转90度的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想用下面的功能位图旋转90度。与它的问题是,它切断部分中的图像的时候的高度和宽度不相等。

注意returnBitmap宽度= original.height和它的高度= original.width

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

 私人位图rotateImage90(位图B)
    {
        位图returnBitmap =新位图(b.Height,b.Width);
        图形G = Graphics.FromImage(returnBitmap);
        g.TranslateTransform((浮点)b.Width / 2,(浮点)b.Height / 2);
        g.RotateTransform(90);
        g.TranslateTransform( - (浮点)b.Width / 2, - (浮点)b.Height / 2);
        g.DrawImage(二,新点(0,0));
        返回returnBitmap;
    }


解决方案

什么<一个href=\"http://web.archive.org/web/20121228011404/http://en.csharp-online.net/Working_with_Images%E2%80%94Using_the_Image_Class\">this:

 私人无效RotateAndSaveImage(字符串输入,字符串输出)
{
    //创建一个对象,我们可以用它来检查图像文件
    使用(图片IMG = Image.FromFile(输入))
    {
        // 90度旋转图片和照片重新保存为JPEG
        img.RotateFlip(RotateFlipType.Rotate90FlipNone);
        img.Save(输出,System.Drawing.Imaging.ImageFormat.Jpeg);
    }
}

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

解决方案

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

这篇关于C#位图旋转90度的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆