The Pictureand StdPicture objects enable you to manipulate bitmaps, icons, metafiles, enhanced metafiles, GIF, and JPEG images assigned to objects having a Picture property.
The Picture property returns or sets a graphic to be displayed in an Ocx object.
Dim X As [New] Picture
Dim X As [New] StdPicture
object.Picture [= picture]
object | : Ocx object |
picture | : Picture object |
X | : Object Variable Name |
A Picture object and the Picture property of a different object (for example, a Form or Image control) behave in an identical fashion and have the same properties and method. The images stored are interchangeable and are transferred using the Set command.
On the surface, the StdPicture and Picture objects (for this section Picture properties and objects should be considered as synonymous) behave identically - they have the same properties and method and the stored images can be transferred between the two using Set. However, under the surface, there is a subtle difference: a Picture object is a COM Interface, while a StdPicture object is a COM class (or coclass).
To quote from the Microsoft site: "A COM interface is a predefined group of related functions that a COM class implements".
Therefore, the Picture object acts as an interface to the StdPicture class object, but is not technically a (co)class object in itself. Hence, there are times when the two do not act identically: for example, trying to use TypeName to describe a Picture object will result in an error because it is not a Picture per se but an interface to one, whereas it works well with an image stored as a StdPicture.
You also need to be careful when transferring either object to a different object or variable type; for example, transferring the Picture object to a Variant will transfer it as an Interface because a Variant can hold a COM Interface, whereas transferring it to a control or object may force a convesion to a (co)class (StdPicture).
There are several ways to load an image into a picture object/property:
Name | Type | Meaning |
---|---|---|
Handle | Handle | Returns the handle to the graphic (bitmap, metafile, icon handle). |
hPal | Handle | Returns the palette handle if available. |
Height | Single | Returns the height of the picture in himets. |
Width | Single | Returns the width of the picture in himets. |
Type | Short | Returns the type of the picture (0=none, 1=bitmap, 2=metafile, 3=icon or cursor, 4=enhanced metafile). |
Render hDC As Handle, x, y, cx, cy As Long, xSrc, ySrc, cxSrc, cySrc As Single, lprcBounds As Handle
hdc | The handle to the destination object's device context. |
x, y | The x- and y-coordinate of upper left corner of the drawing region in the destination object. This coordinate is in the scale units of the destination object. |
cx, cy | The width and height of drawing region in the destination object, expressed in the scale units of the destination object. |
xSrc, ySrc | The x- and y-coordinate of upper left corner of the drawing region in the source object. This coordinate is in HIMETRIC units. |
cxSrc, cySrc | The width and height of drawing region in the source object, expressed in HIMETRIC units. |
lprcbounds | The world bounds of a metafile. This argument should be passed a value of Null unless drawing to a metafile, in which case the argument is passed a user-defined type corresponding to a RECT structure. |
The recommended way to paint part of a graphic into a destination is through the PaintPicture method.
Dim X As Picture
OpenW 1, 0, 0, 300, 300
OpenW 2, 300, 0, 300, 300 : AutoRedraw = 1
Color 255 : PCircle 100, 100, 50
Ocx Command cmd = "Transfer circle to Window 1", 10, 200, 160, 22
Do : Sleep : Until Win_1 Is Nothing Or Win_2 Is Nothing
CloseW 1 : CloseW 2
Sub cmd_Click
cmd.Visible = False
Set X = Win_2.PrintPicture
Win_1.Picture = X
EndSub
When setting the Picture property at design time, the graphic is saved and loaded with the form. If you create an executable file, the file contains the image. When you load a graphic at run time, the graphic isn't saved with the application. Use the SavePicture statement to save a graphic from a form or picture box into a file.
LoadPicture, CreatePicture, PaintPicture, SavePicture
{Created by Sjouke Hamstra; Last updated: 11/07/2023 by James Gaite; Other Contributors: Jean-Marie Melanson}