tags:

views:

74

answers:

3

I have a Graphics object that I've drawn on the screen and I need to save it to a png or bmp file. Graphics doesn't seem to support that directly, but it must be possible somehow.

What are the steps?

+3  A: 

Copy it to a Bitmap and then call the bitmap's Save method.

Note that if you're literally drawing to the screen (by grabbing the screen's device context), then the only way to save what you just drew to the screen is to reverse the process by drawing from the screen to a Bitmap. This is possible, but it would obviously be a lot easier to just draw directly to a Bitmap (using the same code you use to draw to the screen).

MusiGenesis
+1  A: 

You are likely drawing either to an image or on a control. If on image use

    Image.Save("myfile.png",ImageFormat.Png)

If drawing on control use Control.DrawToBitmap() and then save the returned image as above.

Thanks for the correction - I wasn't aware you can draw directly to the screen.

filip-fku
You *can* draw directly to the screen using Graphics, which has a constructor that takes a device context - all you need is the screen's device context.
MusiGenesis
Thanks for this, it has never occurred to me to try drawing straight to the screen!
filip-fku
I would actually recommend *not* drawing directly to the screen, since there's really no point to doing so.
MusiGenesis
+1  A: 
Graphics graph = CreateGraphics();
Bitmap bmpPicture = new Bitmap("filename.bmp");

graph.DrawImage(bmpPicture, width, height);
Cobusve