Friday, July 31, 2009

Creating an Image from a User Control - C#

I wanted to create an image from a "screen shot" of my user control. I wanted to be able to save a picture of the user control at any given moment, save that image to a file for later use.

It's easy!

1. Create a Bitmap object of the size of your control.
Bitmap picture = new Bitmap (800, 600);

2. Create a Graphics object from the Bitmap Object.
Graphics graphx = Graphics.FromImage(picture);

3. Draw your user control to the Graphics object.
CreateControl(graphx);

4. Save your picture to a file.
picture.Save(@"C:\ControlImage.bmp");

It's that simple!

No comments:

Post a Comment