Using thePaintPidure Method

So far we’ve explored the Visual Basic methods for manipulating individual pixels. Manipulating an entire image pixel by pixel is a slow process. In some situations, you may need a quick way to copy an image or part of an image from one container to another. W~dows provides a powerful mechanism for moving pixels around, known as BitBlt (pronounced “BIT BLIT”). BitBlt stands for Bit Block Transfers and is nothing but highly optimized code for moving bits around in memory. VISual Basic programmers can access the services of the BitBlt routines, which are built into the operating system, via the Paintl’icture method. The Paint Picture method allows you to copy a rectangular area of pixels from one object (a PictureBox or Form) onto another. Moreover, the source pixels need not replace the pixels at the destination control. They can be combined with them in various ways to yield interesting effects. The syntax of the PaintPicture method is as follows:

This method accepts a number of arguments, but their use is straightforward:

• The picture argument is the source of the transfer. It’s the Picture property of a PictureBox, an ImageBox, or a Form, whose contents will be transferred to the Picturel control

• DestX and DestY are the coordinates of the transfer’s destination. The rectangle can be transferred anywhere on the destination control.
• DestWidth and DestHeight are the dimensions of the destination area, in pixels.
• SourceX and SourceY are the coordinates of the upper-left comer of the rectangul area to be transferred.
• Source Width and SourceHeight are the dimensions of the rectangle to be copied, in pixels.
• RasterOp specifies how the pixels being transferred will be combined with the existing pixels at the destination.

The most common operation is to replace existing pixeis with the pixels being transferred. But the PaintPicture method lets you combine the source with the destination pixels using the logical operators AND, OR, XOR, and NOT. For eample, you can OR the source pixels with the inverse (NOT) of the destination pixels. Or you can replace the destination pixels with the inverse o( the source pixels. In all, there are 256 ways to combine source and destination pixels, but you’ll never need most of them. Table 7.1 shows a few of the RasterOp argument’s values, which you may find useful in your applications. The (WIles of the constants shown in Table 7.1 are different from the ones listed in Table 6.4, but they have the same effect.

The values of the constants are given in hexadecimal because this is how they appear in Microsoft’s documentation. In your code, you should use the constants listed in the first column of the table. The constant vbSrcCopy overwrites the existing pixels at the destination and is the one you’ll be using most often. To copy the contents of the Picturel PictureBox to thePicturez control, use the following statement:

This rather lengthy statement copies the pixels of the Picturel control onto the Picture2 control. If the two controls don’t have the same dimensions, the image will be distorted to cover the entire destination control. If you don’t want the image to be distorted during the transfer, replace-the arguments Picture2.ScaleWidth and Picture2.ScaleHeight with Picturel.ScaleWidth and PiciurelScalebieighi. If the destination PictureBox is smaller than the source PictureBox, only part of the image will be displayed. If it’s larger, the image won’t fill,it entirely. But in either case, the image won’t be distorted during the process because the width and height arguments are the same on both the source and the destination.

To better Understand how to use the various settings of the RasterOp argument and what effect they have on the destination bitmap, experiment with the various settings and images. In this chapter’s folder on the CD you’ll find the PaintPic project, which lets you combine two bitmaps with the various raster operators, as well as with the remaining arguments of the PaintPicture method.

Scroll to Top