GDI+: how do you render a Graphics object to a bitmap on a background thread?

前端 未结 1 1218
忘掉有多难
忘掉有多难 2021-01-14 16:59

I\'d like to use GDI+ to render an image on a background thread. I found this example on how to rotate an image using GDI+, which is the operation I\'d like to do.



        
相关标签:
1条回答
  • 2021-01-14 17:52

    To draw on a bitmap you don't want to create a Graphics object for an UI control. You create a Graphics object for the bitmap using the FromImage method:

    Graphics g = Graphics.FromImage(theImage);
    

    A Graphics object doesn't contain the graphics that you draw to it, instead it's just a tool to draw on another canvas, which is usually the screen, but it can also be a Bitmap object.

    So, you don't draw first and then extract the bitmap, you create the bitmap first, then create the Graphics object to draw on it:

    Bitmap destination = new Bitmap(200, 200);
    using (Graphics g = Graphics.FromImage(destination)) {
       Matrix rotation = new Matrix();
       rotation.Rotate(30);
       g.Transform = rotation;
       g.DrawImage(source, 0, 0, 200, 200);
    }
    
    0 讨论(0)
提交回复
热议问题