How to Use Windows BitBlt Function From Visual Basic
This article is reprinted from the Microsoft Knowledge Base. To view the article, maximize your help window. This information applies to Visual Basic for Windows, versions 2.0 and 3.0.
Windows GDI.EXE has a function called BitBlt, which will move the
source device given by the hSrcDC parameter to the destination device
given by the hDestDC parameter. This article explains in detail the
arguments of the Windows BitBlt function call.
To use BitBlt within a Visual Basic application, you must Declare the
BitBlt function in the (declaration) section of any code module or within the (declaration) section of a code
window for the form. Declare the Function as follows:
Declare Function BitBlt Lib "GDI" (ByVal hDestDC%,
ByVal X%, ByVal Y%,
ByVal XSrc%, ByVal YSrc%,
ByVal dwRop&) As Integer
Note: The above Declare statement must be written on just one line.
The following formal parameters are defined as:
Formal Parameter Definition
hDestDC Specifies the device context that is to
receive the bitmap.
X,Y Specifies the logical x-coordinate and
y-coordinate of the upper-left corner of the
nWidth Specifies the width (in logical units) of the
destination rectangle and the source bitmap.
nHeight Specifies the height (in logical units) of
the destination rectangle and the source
hSrcDC Identifies the device context from which the
bitmap will be copied. It must be
NULL(zero) if the dwRop& parameter specifies
a raster operation that does not include a
XSrc Specifies the logical x-coordinate and the
y-coordinate of the upper-left corner of the
dwRop Specifies the raster operation to be performed
as defined below.
The following Raster operations are defined using the predefined
constants found in the WINDOWS.H file supplied with the Microsoft
Windows Software Development Kit (SDK). The value
within the "()" is the value to assign to the dwRop& variable.
Code/Value (hex) Description
BLACKNESS (42) Turn output black.
DSINVERT(550009) Inverts the destination bitmap.
MERGECOPY(C000CA) Combines the pattern and the source bitmap
using the Boolean AND operation.
MERGEPAINT(BB0226) Combines the inverted source bitmap with the
destination bitmap using the Boolean OR
NOTSRCCOPY(330008) Copies the inverted source bitmap to the
NOTSRCERASE(1100A6) Inverts the result of combining the
destination and source bitmap using the
Boolean OR operator.
PATCOPY(F00021) Copies the pattern to the destination bitmap.
PATINVERT(5A0049) Combines the destination bitmap with the
pattern using the Boolean XOR operator.
PATPAINT(FB0A09) Combines the inverted source bitmap with the
pattern using the Boolean OR operator.
Combines the result of this operation with
the destination bitmap using the Boolean OR
SRCAND(8800C6) Combines pixels of the destination and source
bitmap using the Boolean AND operator.
SRCCOPY(CC0020) Copies the source bitmap to the destination
SRCERASE(4400328) Inverts the destination bitmap and combines
the results with the source bitmap using the
Boolean AND operator.
SRCINVERT(660046) Combines pixels of the destination and source
bitmap using the Boolean XOR operator.
SRCPAINT(EE0086) Combines pixels of the destination and source
bitmap using the Boolean OR operator.
WHITENESS(FF0062) Turns all output white.
Below is an example of how to copy the contents of a picture control
to the contents of another picture control.
Define a form with two picture controls. Display some graphics on
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: from
the Properties bar, select Picture from the Properties list box and
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.
Add the following code to the Form_Click procedure. Run the program
and click the form. The contents of the first picture will be displayed to
the second picture.
Sub Form_Click ()
' Assign information of the destination bitmap. Note that Bitblt
' requires coordinates in pixels.
Const PIXEL = 3
Picture1.ScaleMode = PIXEL
Picture2.ScaleMode = PIXEL
hDestDC% = Picture2.hDC
X% = 0: Y% = 0
nWidth% = Picture2.ScaleWidth
nHeight% = Picture2.ScaleHeight
' Assign information of the source bitmap.
hSrcDC% = Picture1.hDC
XSrc% = 0: YSrc% = 0
' Assign the SRCCOPY constant to the Raster operation.
dwRop& = &HCC0020
Suc% = BitBlt(hDestDC%, X%, Y%, nWidth%, nHeight%,_
hSrcDC%, XSrc%, YSrc%, dwRop&)