C#将位图旋转90度
我尝试使用以下函数将位图旋转90度。它的问题是,当高度和宽度不相等时,它会切断图像的一部分 请注意返回位图的宽度=original.height,其高度=original.width 谁能帮我解决这个问题或指出我做错了什么C#将位图旋转90度,c#,bitmap,C#,Bitmap,我尝试使用以下函数将位图旋转90度。它的问题是,当高度和宽度不相等时,它会切断图像的一部分 请注意返回位图的宽度=original.height,其高度=original.width 谁能帮我解决这个问题或指出我做错了什么 private Bitmap rotateImage90(Bitmap b) { Bitmap returnBitmap = new Bitmap(b.Height, b.Width); Graphics g = Graphic
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;
}
那么:
错误出现在您对
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
是正确的,因为它将在旋转之前应用
然而,正如鲁本斯·法里亚斯所建议的那样,使用更简单的
RotateFlip
方法可能会更好。我遇到了一个问题,经过一点修改,它就开始工作了。我找到了一些其他的例子,并注意到遗漏了一些对我来说有影响的东西。我必须调用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);
}
}
我正在旋转的位图仅用于显示目的。我从不把它保存到文件中,你不需要保存它;那
RotateFlip
就可以了。您可以使用删除该,并添加返回新位图(img)代码>您可能希望从这里获得一些代码,以确保jpeg以高于默认50的质量级别保存。这仍然为我裁剪了(横向)图像,将图像保留为正方形,并进行了裁剪。。。。
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);
}
}