【问题标题】:Black spots after rotating bitmap c#旋转位图后的黑点c#
【发布时间】: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


【解决方案1】:

由于您的输出是*.png,我假设您想使用透明背景。只需在申请TranslateTransform 之前致电g.Clear(Color.FromArgb(0, default));。或者,如果您愿意,也可以设置任何其他背景颜色。

您也可以设置g.SmoothingMode = SmoothingMode.AntiAlias; 来平滑旋转图像的边。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-01-14
    • 1970-01-01
    相关资源
    最近更新 更多