Making Form Screenshots In .NET

Have you ever need to take a screenshot of your .NET form from code within the form?  Some sort of print screen method?  Don’t touch that PrtSc button just yet, there is another way.

No, I’m not talking about using the gdi32.dll or the Visual Basic Power Pack.  The .NET graphics object has it’s own built in screenshot-taking ability, and ImageSource is here to help show you how to use it.

Getting a screenshot of your form is as easy as instantiating a Bitmap object (sized to your form’s width and height), creating a graphics object from that bitmap and finally calling Graphics object’s CopyFromScreen method.  The code below demonstrates how to do this:

private Bitmap GetFormScreenshot(Form frmRef)
   //Setup the bitmap and graphics objects
   Bitmap bmp = new Bitmap(frmRef.Width, frmRef.Height,
   Graphics gfx = Graphics.FromImage(bmp);

   //Take the screenshot
   gfx.CopyFromScreen(frmRef.Bounds.X, frmRef.Bounds.Y, 0, 0,
       frmRef.Bounds.Size, CopyPixelOperation.SourceCopy);   return bmp;

Wow, it’s just that simple.  Enjoy the rest of your day knowing that you’ll be screenshotting your troubles away.

Leave a Reply

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

You are commenting using your 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