Export (0) Print
Expand All

Visual Basic Reference

Visual Studio 6.0

PaintPicture Method

See Also    Example    Applies To

Draws the contents of a graphics file (.bmp, .wmf, .emf, .cur, .ico, or .dib) on a Form, PictureBox, or Printer. Doesn't support named arguments.

Syntax

object.PaintPicture picture, x1, y1, width1, height1, x2, y2, width2, height2, opcode

The PaintPicture method syntax has these parts:

Part Description
object Optional. An object expression that evaluates to an object in the Applies To list. If object is omitted, the Form object with the focus is assumed to be object.
Picture Required. The source of the graphic to be drawn onto object. Must be the Picture property of a Form or PictureBox.
x1, y1 Required. Single-precision values indicating the destination coordinates (x-axis and y-axis) on object for picture to be drawn. The ScaleMode property of object determines the unit of measure used.
Width1 Optional. Single-precision value indicating the destination width of picture. The ScaleMode property of object determines the unit of measure used. If the destination width is larger or smaller than the source width (width2), picture is stretched or compressed to fit. If omitted, the source width is used.
Height1 Optional. Single-precision value indicating the destination height of picture. The ScaleMode property of object determines the unit of measure used. If the destination height is larger or smaller than the source height (height2), picture is stretched or compressed to fit. If omitted, the source height is used.
x2, y2 Optional. Single-precision values indicating the coordinates (x-axis and y-axis) of a clipping region within picture. The ScaleMode property of object determines the unit of measure used. If omitted, 0 is assumed.
Width2 Optional. Single-precision value indicating the source width of a clipping region within picture. The ScaleMode property of object determines the unit of measure used. If omitted, the entire source width is used.
Height2 Optional. Single-precision value indicating the source height of a clipping region within picture. The ScaleMode property of object determines the unit of measure used. If omitted, the entire source height is used.
Opcode Optional. Long value or code that is used only with bitmaps. It defines a bit-wise operation (such as vbMergeCopy or vbSrcAnd) that is performed on picture as it's drawn on object. For a complete list of bit-wise operator constants, see the RasterOp Constants topic in Visual Basic Help.

There are some limitations in the usage of opcodes.  For example, you can't use any opcode other than vbSrcCopy if the source is an icon or metafile, and the opcodes that interact with the pattern (or "brush" in SDK terms) such as MERGECOPY, PATCOPY, PATPAINT, and PATINVERT actually interact with the FillStyle property of the destination.

Note   Opcode is used to pass a bitwise operation on a bitmap. Placing a value in this argument when passing other image types will cause an "Invalid procedure call or argument" error. This is by design. To avoid this error, leave the Opcode argument blank for any image other than a bitmap.


Remarks

You can flip a bitmap horizontally or vertically by using negative values for the destination height (height1) and/or the destination width (width1).

You can omit as many optional trailing arguments as you want. If you omit an optional trailing argument or arguments, don't use any commas following the last argument you specify. If you want to specify an optional argument, you must specify all optional arguments that appear in the syntax before it.

Note that there is a difference between loading a .Bmp into a PictureBox control, and using the Windows API function BitBlt() to add a picture to it. When you BitBlt an image, the PictureBox control doesn't know to resize like it does if you use the LoadPicture method. Setting the ScaleWidth and ScaleHeight properties to the size of the image also doesn't work. If you want the PictureBox to resize to the new picture after BitBlt'ing, you must do so manually by code, converting units and dealing with borders. Below is a simple example of how to do this:

Sub ResizePictureBoxToImage(pic as PictureBox, twipWd _
  as Integer, twipHt as Integer)
 ' This code assumes that all units are in twips.  If 
 ' not, you must convert it to twips before calling 
 ' this routine.  This also assumes that the image 
 ' was blt'ed to 0,0.
 Dim BorderHt as Integer, BorderWd as Integer
 BorderWd = Pic.Width - Pic.ScaleWidth
 BorderHt = Pic.Height - Pic.ScaleHeight
 pic.Move pic.Left, pic.Top, twipWd + BorderWd, _
   twipHt + BorderHt
End Sub
Show:
© 2014 Microsoft