Search Amazon:

Capture and Print the Screen, a Form, or any Window

This program shows how to capture any form or window, including the screen or desktop window, into a picture object. Then the image can be sent to the printer using the PaintPicture method of Visual Basic's Printer object. This program illustrates how to:

  • Capture the entire contents of a form.
  • Capture the client area of a form.
  • Capture the entire screen.
  • Capture the active window.
  • Capture any portion of any window.
  • Create a Picture object from a bitmap and a palette.
  • Print a Picture object as large as possible on the page.

 

Capture and Print a Form, the Screen, or any Window.
Download Source Code

Capturing and Printing a Window's Contents

Visual Basic contains a Picture object which is really a standard OLE type and is documented in the Control Developer's Kit (CDK). The CDK includes the OleCreatePictureIndirect method which can be used to create new Picture objects. The CreateBitmapPicture procedure in the program calls OleCreatePictureIndirect to build a Picture object from a handle to a bitmap and a handle to a palette. If the Picture includes a valid handle, VB will know how to use it when rendering the Picture to the screen or printer. The CreateBitmapPicture routine is used by the CaptureWindow procedure to construct a Picture object containing a bitmap of all or part of a window.

The CaptureWindow procedure captures any part of a window given a window handle. The routine includes several parameters for describing the exact portion of the window to capture. CaptureWindow 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 copies it. CaptureWindow calls CreateBitmapPicture to build a bitmap from the newly created bitmap and palette.

The CaptureForm, CaptureClient, CaptureScreen, and CaptureActiveWindow procedures all use CaptureWindow to capture specific windows.

Once the desired image is captured it can be printed using the PaintPicture method of the Printer object. Also, the image can easily be copied to the Clipboard using its SetData method.

Notes

This sample is based on Microsoft's Knowledge Base article Q161299.

API Functions Used

This app uses a ton of cool APIs such as: CreateCompatibleBitmap, GetDeviceCaps, GetSystemPaletteEntries, CreateCompatibleDC, CreatePalette, SelectPalette, RealizePalette, BitBlt, GetDC, ReleaseDC, GetWindowRect, GetDesktopWindow and OleCreatePictureIndirect.

Instructions

Run the code and experiment. The Print Picture option scales the captured image to fit on a printed page. The image is sized vertically or horizontally which ever is most appropriate. You can resize the form to better view the captured image.

Sign In
  User Id 
  Password 


Submit Your Own Code and Articles




About TheScarms
About TheScarms

Ask me your programming questions

I read every email and answer all I can.

User Feedback: Comments: 1 Items to Show:     

     
Id:  1 Posted:  9/8/2007 6:58:01 PM By:  SoaringEagle
Very nice little program, works well !!
 
You must log in to post feedback.
Comment:    
 

If you use this code, please mention "www.TheScarms.com"

Email this page


TheScarms AppSentinel lets you securely copy protect and create evaluation versions of your software

TheScarms(tm) AppSentinel lets you quickly and easily create evaluation versions of your software and stop unauthorized copying and unregistered use of your programs!

Get your free
trial copy today!


      The World's Number 1 Web Host

© Copyright 2009 TheScarms