繁体   English   中英

将旋转的图像直接绘制到Graphics对象,而无需创建新的位图

[英]Draw a rotated image directly to Graphics object, without creating a new Bitmap

我可以通过在单独的图形对象上进行绘制来旋转图像,该图形对象在单独的Bitmap上进行绘制,然后在图形对象上绘制位图,从而在最终图像上进行绘制。 这将绘制两个基本相同的图像。 我不想要那个。 该项目是一个简单的图形引擎,因此绘制时间必须尽可能快。 最重要的是,尚未证明System.Graphics在绘制图像方面太擅长。

这是我正在使用的代码。 它需要改进一两个,但是基本方法应该是可见的。

    public static System.Drawing.Bitmap rotatedImage(System.Drawing.Bitmap img, double deg, bool hq)
    {
        int diag = (int)Math.Round(Math.Sqrt(img.Width * img.Width + img.Height * img.Height));
        System.Drawing.Bitmap res = new System.Drawing.Bitmap(diag, diag);
        System.Drawing.Graphics gfx = System.Drawing.Graphics.FromImage(res);
        if (hq) gfx.InterpolationMode = System.Drawing.Drawing2D.InterpolationMode.HighQualityBicubic;
        DoublePair rotCent = new DoublePair(diag / 2, diag / 2);
        gfx.TranslateTransform((float)rotCent.a, (float)rotCent.b);
        gfx.RotateTransform((float)deg);
        gfx.TranslateTransform((-1) * (float)rotCent.a, (-1) * (float)rotCent.b);
        gfx.DrawImage(img, (diag - img.Width) / 2, (diag - img.Height) / 2, img.Width, img.Height);
        return res;
    }

在新的位图上绘制旋转的图像需要旋转用于绘制的图形对象。 我可以做的是将主要图形对象传递给该方法,旋转它以绘制图像,然后将对象旋转回去。 但是我不知道旋转大图像的图形是否比旋转小图像的图形更快或更慢。

那么,哪一个会更快-将整个图形对象旋转两次(旋转以绘制图像,然后向后旋转),或者使用单独的图形绘制旋转的图像,然后与主图形对象一起绘制呢? 是否可以选择旋转图像而不必使用单独的图形对象并且不必旋转图形对象?

我在上一个项目中所做的就是这个,它在Paint事件内部,您不需要创建新的位图,只需重置变换并再次绘制图像即可:

e.Graphics.ResetTransform();
e.Graphics.TranslateTransform(Width/2, Height/2);
e.Graphics.RotateTransform(45f);  //45 degree
e.Graphics.DrawImage(.... //original image

e.Graphics.ResetTransform();
e.Graphics.TranslateTransform(Width/2, Height/2);
e.Graphics.RotateTransform(60f);  //60degree
e.Graphics.DrawImage(.... //original image
...

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM