GDI +: how do you render a Graphics object for a bitmap in a background thread?

I would like to use GDI + to render an image in a background thread. I found this example on how to rotate an image using GDI +, which I would like to do.

private void RotationMenu_Click(object sender, System.EventArgs e)
{
    Graphics g = this.CreateGraphics();
    g.Clear(this.BackColor);
    Bitmap curBitmap = new Bitmap(@"roses.jpg"); 
    g.DrawImage(curBitmap, 0, 0, 200, 200);  

    // Create a Matrix object, call its Rotate method,
    // and set it as Graphics.Transform
    Matrix X = new Matrix();
    X.Rotate(30);
    g.Transform = X;  

    // Draw image
    g.DrawImage(curBitmap, 
    new Rectangle(205, 0, 200, 200), 
        0, 0, curBitmap.Width, 
        curBitmap.Height, 
        GraphicsUnit.Pixel);  

    // Dispose of objects
    curBitmap.Dispose();
    g.Dispose(); 
} 

My question has two parts:

  • How would you do this.CreateGraphics()in the background thread? Is it possible? I understand that the UI object thisin this example. So, if I do this processing in the background thread, how would I create a graphic?

  • How can I extract a bitmap from a Graphics object that I use when I'm done processing? I could not find a good example of how to do this.


: , ? - , , . !

+3
1

, Graphics . Graphics FromImage:

Graphics g = Graphics.FromImage(theImage);

A Graphics , , , , Bitmap.

, , , , Graphics :

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);
}
+12

Source: https://habr.com/ru/post/1709225/


All Articles