dcsimg
November 27, 2020
Hot Topics:

More Windows Secrets for Visual Basic

  • By Karl Moore
  • Send Email »
  • More Articles »

Capturing the Screen, Quick and Easy

When it comes to showing people how to capture the screen, most VB.NET authors I've seen tend to revert to the old-school method of working: using the API. There is, however, a better way.

The following function is called GetScreenCapture and returns an Image object. It captures the screen by running a series of "Print Screen" key presses, which puts a screen grab on the Clipboard, ready for my nifty little function to devour and return. My function accepts a FullScreen argument, too: Pass True to capture the whole screen, or False to capture just the active window.

Here's the code:

Public Function GetScreenCapture( _
   Optional ByVal FullScreen As Boolean = False) As Image
   ' Captures the current screen and returns as an Image
   ' object
   Dim objSK As SendKeys
   Dim imgCapture As Image
   If FullScreen = True Then
       ' Print Screen pressed twice here as some systems
       ' grab active window "accidentally" on first run
       objSK.SendWait("{PRTSC 2}")
   Else
       objSK.SendWait("%{PRTSC}")
   End If
   Dim objData As IDataObject = Clipboard.GetDataObject()
   Return objData.GetData(DataFormats.Bitmap)
End Function

And here are a couple of examples demonstrating how to use that Image object—firstly, saving it as a file; and secondly, using it to set the Image property of a PictureBox control:

GetScreenCapture(True).Save("c:\screengrab.bmp")
PictureBox1.Image = GetScreenCapture()

My sample application, capturing the active window (again and again)

Next Time

Coming up in part three of Windows Secrets, check out:

  • How to 'Reset' a Form
  • The Facts on Visual Inheritance
  • Secrets of Working with the System Tray
  • Best of All Worlds: Creating an Ultra-Thin Client

See you next time!

# # #





Page 5 of 5



This article was originally published on January 29, 2003

Enterprise Development Update

Don't miss an article. Subscribe to our newsletter below.


Thanks for your registration, follow us on our social networks to keep up-to-date