在 C# 中旋转图形/图像

rotating Graphics/ image in c#

我编写了以下代码来尝试旋转位图(这是一个测试)我的想法是获取位图并将其旋转一定程度,然后使用 win 表单将其绘制在屏幕上

protected override void OnDoubleClick(EventArgs e)
    {
        base.OnDoubleClick(e);
        Graphics g = this.CreateGraphics();
        Bitmap b = new Bitmap(path);
        g.Clear(Color.White);
        imagePosition = Cursor.Position;
        b = RotateImage(b, 45);
        g.DrawImage(b, new Point(100, 100));
    }
    public Bitmap RotateImage(Bitmap b, float angle)
    {
        Bitmap returnBitmap = new Bitmap(b.Width, b.Height);
        returnBitmap.SetResolution(b.HorizontalResolution, b.VerticalResolution);
        Graphics g = Graphics.FromImage(returnBitmap);
        g.TranslateTransform((float)b.Width / 2, (float)b.Height / 2);
        g.RotateTransform(angle);
        g.TranslateTransform(-(float)b.Width / 2, -(float)b.Height / 2);
        g.DrawImage(b, new Point(0, 0));
        return returnBitmap;
    }

这是旋转前的图像

这是旋转 45 度后的图像,如代码所示

它被剪裁的原因是没有足够的 space 来显示它旋转。对角线比边长(毕达哥拉斯)。

您需要为图像制作更多space,然后它应该显示正常。你如何做取决于图像中包含的内容。