Microsoft KB Archive/77060

= How to Print a VB Picture Control Using Windows API Functions =

Article ID: 77060

Article Last Modified on 12/12/2003

-

APPLIES TO


 * Microsoft Visual Basic 2.0 Standard Edition
 * Microsoft Visual Basic 3.0 Professional Edition
 * Microsoft Visual Basic 2.0 Professional Edition
 * Microsoft Visual Basic 3.0 Professional Edition
 * Microsoft Visual Basic 1.0 Standard Edition

-



This article was previously published under Q77060



SUMMARY
This article explains how to print a Visual Basic picture control to a printer using several Windows API function calls.

NOTE: this example will not work correctly on PostScript printers. Instead of the picture control printing, two blank sheets are ejected from the printer when using a printer configured to use the PostScript printer driver. For the example to work correctly, the printer must use a standard non-PostScript laser printer configuration (such as PCL/HP.)

For additional information on printing to PostScript printers, please see the following article in the Microsoft Knowledge Base:

85978 : Print Form or Client Area to Size on PostScript or PCL Printer



MORE INFORMATION
To print a picture control from Visual Basic, you must use the PrintForm method. Although this can very useful, there is no straightforward method of printing just a picture control without the use of API function calls. Printing a picture control to the printer is useful when you want to control the location or size of the printed image. Calling API functions to print a picture control is also useful if you want to include other images or text along with the picture image on a single sheet of paper.

To print a bitmap, you need to do the following:


 * 1) Create a memory device context that is compatible with the bitmap (CreateCompatibleDC). A memory device context is a block of memory that represents a display surface. It is used to prepare images before copying them to the actual device surface of the compatible device.
 * 2) Save the present object (SelectObject) and select the picture control using the handle from the memory device context.
 * 3) Use the BitBlt or StretchBlt function to copy the bitmap from the memory device context to the printer.
 * 4) Remove the bitmap from the memory device context (SelectObject) and delete the device context (DeleteDC).

Step-by-Step Example
The following steps demonstrate this process:

 Start Visual Basic or from the File menu, choose New Project (ALT, F, N) if Visual Basic is already running. Form1 is created by default. Add a picture control (Picture1) to Form1 and set the AutoRedraw property to True. Add a command button (Command1). Display some graphics in Picture1 by loading from a picture file or pasting from the Clipboard at design time. You can load a picture from a file as follows:

 Select the Picture property from the Properties bar. Click the arrow at the right of the Settings box, then select the desired picture file (such as a .BMP or .ICO file supplied with Microsoft Windows) from the dialog box.</ol> </li>  Add the following declarations to the global Declarations section of the Code window. Enter each Declare statement as one, single line. Declare Function CreateCompatibleDC% Lib &quot;GDI&quot; (ByVal hDC%)

Declare Function SelectObject% Lib &quot;GDI&quot; (ByVal hDC%, ByVal hObject%)

Declare Function StretchBlt% Lib &quot;GDI&quot; (ByVal hDC%, ByVal X%,     ByVal Y%, ByVal nWidth%, ByVal nHght%, ByVal hSrcDC%, ByVal XSrc%,      ByVal YSrc%, ByVal nSrcWidth%, ByVal nSrcHeight%, ByVal dwRop&amp;)

Declare Function DeleteDC% Lib &quot;GDI&quot; (ByVal hDC%)

Declare Function Escape% Lib &quot;GDI&quot; (ByVal hDC As Integer,     ByVal nEscape As Integer, ByVal nCount As Integer,      LpInData As Any, LpOutData As Any) </li>  Add the following code to the Command_Click event:

Sub Command1_Click Const SRCCOPY = &HCC0020 Const NEWFRAME = 1 Const PIXEL = 3

'* Display hour glass. MousePointer = 11 Picture1.Picture = Picture1.Image

'* StretchBlt requires pixel coordinates. Picture1.ScaleMode = PIXEL Printer.ScaleMode = PIXEL

Printer.Print &quot;&quot;

hMemoryDC% = CreateCompatibleDC(Picture1.hDC) hOldBitMap% = SelectObject(hMemoryDC%, Picture1.Picture)

'Enter the following three lines as one, single line: ApiError% = StretchBlt(Printer.hDC, 0, 0, Printer.ScaleWidth,        Printer.ScaleHeight, hMemoryDC%, 0, 0, Picture1.ScaleWidth,         Picture1.ScaleHeight, SRCCOPY)

hOldBitMap% = SelectObject(hMemoryDC%, hOldBitMap%) ApiError% = DeleteDC(hMemoryDC%)

Result% = Escape(Printer.hDC, NEWFRAME, 0, 0&, 0&)

Printer.EndDoc

MousePointer = 1 End Sub </li> Run the program to copy the bitmap to the printer. If you have selected a low resolution from the Print Manager, printing the bitmap will proceed quickly (the lower the resolution, the faster the print time). While designing your software, you may want to keep this at the lowest possible resolution. The print resolution can be changed from the Windows Control Manager.</li></ol>

<div class="references_section">