Screen Capture and Thumbnail Creation from Visual Basic 6
You are here : Tutorials / Visual Basic
First Published Oct 2004, Reviewed Dec 2006
Introduction
We create lots of thumbnail images of web site screen shots, for our documentation and for use on our web site. Until we wrote this program we have been manually hitting the windows PrtSc button, to copy the image to the clipboard. Then firing up Photoshop, selecting paste image, using Photoshop to resize the image, and finally saving out the thumbnail as a JPEG.
When you create a large amount of thumbnail images this becomes too time intensive, so we wrote a small Visual Basic application to grab the current screen shot, and resample the captured image as a thumbnail automatically. This short tutorial outlines how the code was written, so that if you need to add screen capture functionality to your project it will give you a head start.
The sample thumbnails on this page were auto generated using this software.
The Form Components Needed
To write this software you need a form with 3 components added to it:-
- A PictureBox named
ScreenCapture - A CommandButton to actually perform the screen capture and thumbnail generation
- A CommonDialog control to produce a SaveAs, dialog so that we can name the thumbnail JPEG
Capturing the Screen to the Clipboard
The first task is to work out how to programmatically capture the screen by simulating pressing the print screen button on your keyboard. For some reason VB's SendKeys command doesnt appear to work. Luckily we can use the keybd_event function from user32.dll, so first of all we need to declare this function in a module.
Option Explicit
Declare Sub keybd_event Lib "user32" _
(ByVal bVk As Byte, ByVal bScan As Byte, _
ByVal dwFlags As Long, ByVal dwExtraInfo As Long)
You can capture the whole screen by calling the keybd_event function like so :-
keybd_event vbKeySnapshot, 0, 0, 0
Now that we can capture the whole screen, we can write the first part of the code for the CommandButton on Click event. Which will capture the screen to the clipboard, paste the contents into the PictureBox and then save the image to disk for later processing into a thumbnail.
Private Sub cmdCapture_Click()
' hide the form
' (as we dont want this in the screen shot)
Me.Visible = False
DoEvents
Clipboard.Clear
' send a print screen button keypress event
' and DoEvents to allow windows time to process
' the event and capture the image to the clipboard
keybd_event vbKeySnapshot, 0, 0, 0
DoEvents
' send a print screen button up event
keybd_event vbKeySnapshot, 0, &H2, 0
DoEvents
' paste the clipboard contents into the picture box
ScreenCapture.Picture = Clipboard.GetData(vbCFBitmap)
DoEvents
' show the form and change the pointer to an
' hourglass while the image is processed
Me.Visible = True
Me.Refresh
Screen.MousePointer = vbHourglass
' save the image to a file using the application path
SavePicture ScreenCapture.Picture, App.path & "\screen.bmp"
The image file screen.bmp is a bitmap image containing the whole screen shot at what ever resolution your monitor is set at. The next step is to read in screen.bmp, resample it down to a manageable thumbnail size and save the results to a new image, preferably a JPEG.
Creating the Thumbnail Image
We reviewed a few image processing libraries which offered reading and writing of different file formats, and offered the BiCubic resampling we were after, and finally decided to use FreeImage which comes as a Windows DLL and has some impressive features.
We can now finish off the code for the capture button click event. Using FreeImage to read in the file screen.bmp (saved from the picture box), we can use the FreeImage_Rescale function to create the thumbnail and FreeImage_Save to create the JPEG file.
Dim FreeImage1 As Long
Dim FreeImage2 As Long
Dim bOK As Long
' use the FreeImage.dll (http://freeimage.sourceforge.net/)
' to load the screen image
FreeImage1 = FreeImage_Load(FIF_BMP, App.path & "\screen.bmp", 0)
' resize the image to 150x113 pixels
' the last parameter 2 tells the function to use the
' BiCubic resample method. See the documentation for
' the other resampling methods available.
FreeImage2 = FreeImage_Rescale(FreeImage1, 150, 113, 2)
' show a SaveAs dialog box
CommonDialog.filename = "xxx_thumb.jpg"
CommonDialog.filter = "jpeg"
CommonDialog.ShowSave
' save the thumbnail as an JPEG image with high quality
bOK = FreeImage_Save(FIF_JPEG,
FreeImage2, CommonDialog.filename, &H80)
As you can see from the samples on this page the results are very good. If you would like to use this code and expand on it, for example automate screen shot images for documentation then you can download, the code from the link below.
Now Available as Freeware
Many people asked us to release this as an actual software product, so you can now download our Screen Capture and Thumbnail Creation Software.
