Capture a picture from the screen
By: Microsoft Corporation Date:11/01/2001


3.95 KB

Download now!

The included module provides several useful routines for capturing images.
The routines in the example can:

The CaptureWindow routine in the example captures any portion of a window given a window handle. The routine includes several parameters for describing the exact portion of the window to capture. Capture Window works by copying the on-screen image of a window into a new bitmap. It also checks to see if the screen has a palette and if so it makes a copy of it. CaptureWindow then calls CreateBitmapPicture to construct a bitmap from the newly created bitmap and palette.

The CaptureForm, CaptureClient, CaptureScreen, and CaptureActiveWindow routines included in the example all use CaptureWindow to capture specific windows. CaptureForm and CaptureClient both call Capture window and pass it the hWnd property of a Form object. CaptureScreen simply gets the handle to the desk top window and calls CaptureWindow. Similarly, CaptureActiveWindow just gets the window handle of the active window and calls CaptureWindow.


CaptureWindow(hWndSrc, Client, LeftSrc, TopSrc,
WidthSrc, HeightSrc)


frmSrc: form object to be printed
hWndSrc: handle of an external window to print
Client: boolean used to decide wether to capture the whole window or just the client area
LeftSrc: left co-ordinate of the area to be captured (in pixels)
TopSrc: top co-ordinate of the area to be captured (in pixels)
WidthSrc: width of the area to be captured (in pixels)
HeightSrc: height of the area to be captured (in pixels)

Type of data

frmSrc: Form (Object)
hWndSrc: Long
Client: Boolean
LeftSrc: Long
TopSrc: Long
WidthSrc: Long
HeightSrc: Long


A Picture object that can be assigned to any Picture property (of Image or PictureBox controls), copied to the Clipboard (using SetData method) or printed on paper (using the PaintPicture method).


Example 1:
Capture the entire screen.

Set Picture1.Picture = CaptureScreen()

Example 2:
Capture the entire form, including title and border.

Set Picture1.Picture = CaptureForm(Me)

Exaple 3:
Capture the entire form, but just the client area.

Set Picture1.Picture = CaptureClient(Me)


Once the desired image is captured in a Picture object, it is easy to print  using the PaintPicture method of the Printer object. The module provides the routine PrintPictureToFitPage that uses the PaintPictureMethod to print the captured images as large as possible in the printable area of the page.



PrintPictureToFitPage(Prn, Pic)


Prn: Printer object used to print
Pic: Picture object to print

Type of data

Prn: Printer (Object)
Pic: Picture (Object)




Before running this functions verify in Project References that a Reference to OLE Automation exists (StdOle2.tlb).

Click Here!

Visual Basic Programming Zone is a website by Lorenzo Dalla Vecchia.
To contact the Webmaster, click here.
Hosted by InWind: