Graphics attract users and are an important part of any application. A graphic can be a simple icon or a complex bitmap. This section describes some techniques for manipulating the display of bitmaps using the Win32 APIs. In this section, we’ll look at the API graphics functions, BitBlt() and StretchBlt() which are more or less equivalent to the PaintPicture method of the graphics controls of VisualBasic. The main difference is that the PaintPicture method applies to a given control (Picture- Boxor Form), but the BitBlt() and StretchBlt() functions can copy pixels from anywhere on the Desktop. As a result, you can use them for applications, such as screen capture programs, for which the PaintPicture method is inadequate .
The BitBlt() Function
The BitBlt() function is used to blit, or copy, an image from a to a destination object. The function transfers a rectangle of pixels from the specified source device context into a destination device context. Earlier in this chapter, the MenuBMP application demonstrated the use of BitBlt(). Now, let’s look at it in detail and compare it with the PaintPicture method provided with VisualBasic.
Declare the BitBlt() function as follows:
unlike the PaintPicture method, the BitBlt() function ‘requires you to supply all the arguments. You must supply the startingX and Ycoordinates, the height an width, and the starting coordinates of the source.
Before you can use the BitBltn function, you must create a devicecontext for the source and destination objects with the CreateCompatibleDC() and Create- CompatibleBitmap() functions. If you plan to use the mmlt() function to copy from a PictureBox control, the control’s device context is its hWnd property. However, the BitBlt function can be used with any device context, even with the device context of a bitmap loaded into memory.
With the exceptions of hDestDC and hSrcDC, the remaining arguments of the BitBlt() function are the same as those of the PaintPicture method. They specify the origin of the rectangle to be copied (the source arguments) and the origin and dimensions of the rectangle to which the pixels will be copied to (the destination arguments). The dimensions of the source and destination rectangles must be the same. Table 13.7 lists the raster operation codes for the dwRop argument. These codes define how the pixels of the source rectangle are combined with the pixels of the destination rectangle.
The StretchBlt() Function
Another way to draw bitmaps is to use the StretchBIt() function. with this function, you can copy a bitmap from a source rectangle into a destination rectangle, stretching or compressing the bitmap to fit the destination rectangle! Here’s the declaration for StretchBlt():
As you can see, StretchBlt() has all the arguments of the BitBlt() function plus two more arguments that specify the size of the source rectangle. StretchBltO can compress a picture, stretch it backward, or turn it inside out by using different signs for the arguments. The raster operation codes in Table 13.7 define how Windows combines the colors of the Source and destination rectangle to achieve the final color.
These settings are also explained in Chapter 7, Mlmipuiating Color and Pixels with Visual Basic. You can also open the PaintPic application discussed in Chapter 7 to experiment-with various settings of the raster operation codes. The PaintPic application uses the PaintPicture method, but the raster operations are the same.