Visual Basic 6's PaintPicture method allows you to create special effects with bitmap images. You must start with a source picture, which can be an image in either a Picture Box control or on a form. Then, you can take all or part of that picture—any rectangular region, in fact—and place it anywhere you desire on another Picture Box or form, or on the source Picture Box or form. You have numerous options as to how the copied image is combined with any image that already exists in the destination. In addition, you can stretch or shrink the "copied" picture.
Here are the arguments of the PaintPicture method:
object.PaintPicture source, x1, y1, w1, h1, x2, y2, w2, h2, opcode
- object: The name of the Picture Box, Form, or Printer object where the picture is to be placed. This argument is optional. If it's omitted, the form with the focus is assumed.
- source: The source of the graphic. It must be the Picture property of a Form or Picture Box object.
- x1, y1: Single-precision values indicating the destination coordinates (in other words, the location on the destination object where the top-left corner of the image is to be drawn). The ScaleMode property of the object determines the unit of measure used.
- w1, h1: Single-precision values indicating the destination width and height of the picture, using units specified by the ScaleMode property of the destination object. If the destination width and/or height is larger or smaller than the source width (w2) or height (h2), the picture is stretched or compressed to fit. These arguments are optional; if you omit them, the source width (w1) and height (h1) are used with no stretching or compression.
- x2, y2: Single-precision values indicating the source coordinates of the region in the source object that is to be copied (in units specified by the source object's ScaleMode property). These arguments are optional; if you omit them, 0 is assumed (indicating the top-left corner of the source image).
- w2, h2: Single-precision values indicating the width and height of the region within the source that is to be copied (in units specified by the source object's ScaleMode property). These arguments are optional; if you omit them, the entire source width and height are used.
- opcode: A type Long value that defines the bit-wise operation that is performed between the pixels of the source picture and the pixels of any existing image on the destination. This argument, which is optional, is useful only with bitmaps. If you omit the argument, the source is copied onto the destination, replacing anything that is there.
Think of the source picture as being printed on a rubber sheet. You can cut out any rectangular part of the picture you want, stretch it vertically and/or horizontally, and stick it down anywhere on the destination picture.
Making sense of opcode
The opcode argument can be a bit confusing because there are dozens of ways that you can copy a picture from one location to another. Here are more details about how it works.
In the destination object, where the copied picture is going to be placed, an image already exists. A bitmap picture may be displayed there already, but even if the destination is blank, something is there—perhaps just white pixels. Over the extent of the copied region, for each pixel in the copied image there is a corresponding pixel at the same location in the destination. How will these pixels be combined? This is what the opcode argument determines.
The simplest (and perhaps most common) operation is to have each new pixel replace the original pixel. The opcode argument for this is the predefined constant vbSrcCopy. Other opcode arguments perform a variety of logical combinations of the source picture with the destination picture. These can be used to create certain types of visual effects, but I won't go into them here. You can explore the topic further in the Visual Basic Help system.
Advance your scripting skills to the next level with TechRepublic's free Visual Basic newsletter, delivered each Friday. Automatically sign up today!