简体   繁体   English

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

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

I was able to make an image to rotate by drawing on a separate graphics object, which draws on a separate Bitmap, and then draw the bitmap with the graphics object, which draws on the final image. 我可以通过在单独的图形对象上进行绘制来旋转图像,该图形对象在单独的Bitmap上进行绘制,然后在图形对象上绘制位图,从而在最终图像上进行绘制。 This is drawing two basically same images. 这将绘制两个基本相同的图像。 And I don't want that. 我不想要那个。 The project is a simple graphics engine, so drawing time must be as fast as possible. 该项目是一个简单的图形引擎,因此绘制时间必须尽可能快。 On top of that, System.Graphics has not proven to be too good at drawing images. 最重要的是,尚未证明System.Graphics在绘制图像方面太擅长。

Here is the code I am using. 这是我正在使用的代码。 It needs an improvement or two, but the basic method should be visible. 它需要改进一两个,但是基本方法应该是可见的。

    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;
    }

Drawing a rotated image on a new Bitmap requires to rotate the graphics object, which is used for drawing. 在新的位图上绘制旋转的图像需要旋转用于绘制的图形对象。 What I can do is pass the main graphics object to the method, rotate it to draw the image and then rotate the object back. 我可以做的是将主要图形对象传递给该方法,旋转它以绘制图像,然后将对象旋转回去。 But I don't know if rotating a graphics for a large image is faster or slower than rotating a graphics for a small image. 但是我不知道旋转大图像的图形是否比旋转小图像的图形更快或更慢。

So, which is going to be faster - to rotate the whole graphics object twice (rotate to draw the image and then rotate back), or to draw the rotated image with a separate graphics and then draw it with the main graphics object? 那么,哪一个会更快-将整个图形对象旋转两次(旋转以绘制图像,然后向后旋转),或者使用单独的图形绘制旋转的图像,然后与主图形对象一起绘制呢? Is there an option to rotate an image without having to use a separate graphics object and without having to rotate a graphics object? 是否可以选择旋转图像而不必使用单独的图形对象并且不必旋转图形对象?

What I did in the last project was this one, this is inside Paint event, you dont need to create new bitmap, just reset transform and draw the image again: 我在上一个项目中所做的就是这个,它在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