Need a break from coding? - Then try a boating holiday in Europe.
Published : 12.Oct.2004
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.
To write this software you need a form with 3 components added to it:-
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.
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.
Bookmark this article at :-
If the information you have found here helps to strengthen your business, or has saved you lot's of time, and you'd like to show your appreciation, consider making a small donation.