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

  1. Rufor - April 15, 2009

    Hi,
    Interesting, I`ll quote it on my site later.

    Thank you
    Rufor

  2. vikas - May 29, 2009

    how we can reduce the size of image captured programatically in C#. if you can tell me about that.

  3. JaneRadriges - June 13, 2009

    The article is ver good. Write please more

  4. KattyBlackyard - June 14, 2009

    Great post! I’ll subscribe right now wth my feedreader software!

  5. George Messeha - June 26, 2009

    Very helpful post. Thank you :)

  6. cialis - July 16, 2009

    Great topic. Now i can say thank you

  7. Tyler - July 16, 2009

    Hey, can I share your post with my readers? Let me know if it’s okay or not.

  8. crow - July 16, 2009

    Yeah, go ahead and share.

  9. soundtracks - July 17, 2009

    +1

  10. javagame - July 17, 2009

    Very goood!!!

  11. geekonweb - September 9, 2009

    Nice trick … this is what I was looking .

    Thanks a lot :-)

Leave a Reply