【发布时间】:2022-07-10 23:33:23
【问题描述】:
在进一步操作之前,我需要将图像旋转一定角度。旋转后,我的位图上留下了黑色区域。
Image after rotating by 30 degrees
我已经设法在 java right here 中找到了类似的问题。 c#有类似的东西吗?
我正在使用我之前在堆栈中找到的以下代码:
Bitmap Rotate_Image(Bitmap bmp, float angle)
{
Bitmap rotatedImage = new Bitmap(bmp.Width, bmp.Height);
rotatedImage.SetResolution(bmp.HorizontalResolution, bmp.VerticalResolution);
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;
}
【问题讨论】:
-
“斑点”是什么意思?
-
你可以给我们一些截图吗?您发布的那个看起来像一个稍微模糊的旋转 E。
-
为什么你认为角落不是黑色的?
-
斑点是指旋转后留下的黑色区域。截图在第一个链接。关于颜色,问题是,是否可以将默认的黑色更改为其他颜色 - 例如白色?
-
在编写旋转后的图像之前,您可以
g.Clear(Color.White)使用您想要的背景颜色吗?见:stackoverflow.com/questions/4551316/…
标签: c# .net-core bitmap rotation