How To Get Screen Shot Capture in C#
It’s often necessary to programmatically get a screen shot of the desktop and save it to a file. This is pretty easy with C#; we can create a simple method that will save an image of whatever is on the screen or desktop.
Saving a Bitmap Image of the Screen
First make sure you include the System.Drawing.Imaging namespace. Now, all we really have to do is create a new bitmap and use the CopyFromScreen method of the bitmaps Graphics device.
/// <summary> /// Saves a screen capture in the specified image format to a file. /// </summary> /// <param name="filename"></param> /// <param name="format"></param> private void SaveScreenShot(string filename, ImageFormat format) { Bitmap screenShot = CaptureScreenShot(); screenShot.Save(filename, format); } /// <summary> /// Saves a picture of the screen to a bitmap image. /// </summary> /// <returns>The saved bitmap.</returns> private Bitmap CaptureScreenShot() { // get the bounding area of the screen containing (0,0) // remember in a multidisplay environment you don't know which display holds this point Rectangle bounds = Screen.GetBounds(Point.Empty); // create the bitmap to copy the screen shot to Bitmap bitmap = new Bitmap(bounds.Width, bounds.Height); // now copy the screen image to the graphics device from the bitmap using (Graphics gr = Graphics.FromImage(bitmap)) { gr.CopyFromScreen(Point.Empty, Point.Empty, bounds.Size); } return bitmap; }
And that’s really all there is to capturing a screen shot. You can save it to whatever file you would like and in any of the image formats supported by ImageFormat such as PNG or JPEG. Keep in mind if your working in a multidisplay environment you’ll want to be careful which display your targeting. Or perhaps you may want a huge image of all the desktop displays.
You can use the above code simply by calling the SaveScreenShot method with a file name and image format to save to:
//save screen shot in PNG image format SaveScreenShot("MyScreenShot.png", ImageFormat.Png);
With a really small change you can modify the above code to save a screen shot of an arbitrary control instead of the entire desktop display.
Saving a Screen Capture of a Control
/// <summary> /// Saves a screen capture of the specified control to a file. /// </summary> /// <param name="control"></param> /// <param name="filename"></param> /// <param name="format"></param> private void SaveControlShot(Control control, string filename, ImageFormat format) { Bitmap controlShot = CaptureControlShot(control); controlShot.Save(filename, format); } /// <summary> /// Gets a bitmap image of the specified control. /// </summary> /// <param name="control"></param> /// <returns></returns> private Bitmap CaptureControlShot(Control control) { // get control bounding rectangle Rectangle bounds = control.Bounds; // create the new bitmap that holds the image of the control Bitmap bitmap = new Bitmap(bounds.Width, bounds.Height); // copy controls image data to the bitmaps graphics device using (Graphics gr = Graphics.FromImage(bitmap)) { gr.CopyFromScreen(control.Location, Point.Empty, bounds.Size); } return bitmap; }
And you can use this just as easily as the previous screen capture method:
// if "this" is a control, like a form SaveControlShot(this, "MyControlShot.png", ImageFormat.Png);
And there you have it, an example of simple methods for saving a screen shot or an image of a control. I Hope you find this helpful.
April 13, 2009
Posted in: C#, Programming

11 Responses
Hi,
Interesting, I`ll quote it on my site later.
Thank you
Rufor
how we can reduce the size of image captured programatically in C#. if you can tell me about that.
The article is ver good. Write please more
Great post! I’ll subscribe right now wth my feedreader software!
Very helpful post. Thank you
Great topic. Now i can say thank you
Hey, can I share your post with my readers? Let me know if it’s okay or not.
Yeah, go ahead and share.
+1
Very goood!!!
Nice trick … this is what I was looking .
Thanks a lot
Leave a Reply