Home > Posts > HowTo: Save screenshot of a control hosted on a WinForm

HowTo: Save screenshot of a control hosted on a WinForm

I’m trying to automate grabbing of activity screenshots from ClipFlair Activity Gallery, but most command-line screen capturing tools, like SiteShoter and IECapt fail to get an image from Silverlight content (they get just a background color).

Some other opensource tool that I could tweek was ThumbPage, but  that didn’t support command-line and needed to disable Silverlight hardware acceleration in the webpage source (that is not define enableGPUAcceleration Silverlight param or set it to false) to grab the Silverlight content too in the image. This is probably because that tool was written in VB6 and must be using plain GDI instead of GDI+ or other newer graphics API, but I’m not sure if that’s the issue with the h/w acceleration.

So, I started making my own WinForms-based tool in Visual Studio. The following sample code is based on ArsenMkrt’s reply at StackOverflow, but this one allows you to capture a control in your form (my tool has a WebBrowser control in it and want to capture just its display). Note the use of PointToScreen method:

//Project: WebCapture
//Filename: ScreenshotUtils.cs
//Author: George Birbilis (http://zoomicon.com)
//Version: 20130820

using System.Drawing;
using System.Windows.Forms;

namespace WebCapture
  public static class ScreenshotUtils

    public static Rectangle Offseted(this Rectangle r, Point p)
      return r;

    public static Bitmap GetScreenshot(this Control c)
      return GetScreenshot(new Rectangle(c.PointToScreen(Point.Empty), c.Size));

    public static Bitmap GetScreenshot(Rectangle bounds)
      Bitmap bitmap = new Bitmap(bounds.Width, bounds.Height);
      using (Graphics g = Graphics.FromImage(bitmap))
        g.CopyFromScreen(new Point(bounds.Left, bounds.Top), 
                         Point.Empty, bounds.Size);
      return bitmap;

    public const string DEFAULT_IMAGESAVEFILEDIALOG_TITLE = "Save image";
"PNG Image (*.png)|*.png|JPEG Image (*.jpg)|*.jpg|Bitmap Image (*.bmp)|
*.bmp|GIF Image (*.gif)|*.gif";

//*** NOTE: the string above is a single line, split it for posting here ***//

    public static SaveFileDialog GetImageSaveFileDialog(
      SaveFileDialog dialog = new SaveFileDialog();

      dialog.Title = title;
      dialog.Filter = filter;

      return dialog;

    public static void ShowSaveFileDialog(this Image image, 
                                          IWin32Window owner = null)
      using (SaveFileDialog dlg = GetImageSaveFileDialog())
        if (dlg.ShowDialog(owner) == DialogResult.OK)


Note the “using” clause above, it makes sure that the local variable g of type Graphics is Disposed immediately after the respective clause exits, instead of leaving any allocated resources for the garbage collector to handle. Plus the compiler makes sure the “g” variable isn’t use at the following code in that method by mistake (which could occur if you disposed it there by hand instead).

Having the Bitmap object you can just call Save on it:

private void btnCapture_Click(object sender, EventArgs e)
  webBrowser.GetScreenshot().Save("C://test.jpg", ImageFormat.Jpeg);

The above assumes the Garbage Collector (aka GC) will grab the bitmap, but maybe it’s better to assign the result of someControl.getScreenshot() to a Bitmap variable, then dispose that variable manually when finished, especially if you’re doing this grabbing often (say you have a list of webpages you want to load and save screenshots of them):

private void btnCapture_Click(object sender, EventArgs e)
  Bitmap bitmap = webBrowser.GetScreenshot();
  bitmap.Dispose(); //release bitmap resources



Now WebCapture tool is ClickOnce-deployed from the web (also has nice autoupdate support thanks to ClickOnce) and you can find its source code at http://ClipFlair.codeplex.com.

Speaking of ClickOnce, I noticed that if at the autoupdate dialog shown when you launch the app from its desktop shortcut (and there’s a new version available) you press “Skip”, it won’t offer you this update anymore when you launch an app (I hope that when an even newer update is there it will prompt you), but you can override that behaviour by going to the installation page for your ClickOnce app (where you installed it from in the first place) and press the “Install” button there.


Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: