Partager via


Visual Basic Concepts

Using the Picture Object

The Picture object is similar in some respects to the Printer object — you can’t see it, but it’s useful nonetheless. You could think of the Picture object as a invisible picture box that you can use as a staging area for images. For example, the following code loads a Picture object with a bitmap and uses that bitmap to set the Picture property of a picture box control:

Private Sub Command1_Click()
   Dim objPic As Picture
   Set objPic = LoadPicture("Butterfly.gif")
   Set Picture1.Picture = objPic
End Sub

The Picture object supports bitmaps, GIF images, JPEG images, metafiles, and icons.

Using Arrays of Picture Objects

You can also use an array of Picture objects to keep a series of graphics in memory without using a form that contains multiple picture box or image controls. This is convenient for creating animation sequences or other applications where rapid image changes are required. Declare the array at the module level:

Dim objPics(1) As Picture

Add the following code to the Form_Load event:

' Load bitmaps int the Picture object array.
Set objPics(0) = LoadPicture("Butterfly1.gif")
Set objPics(1) = LoadPicture("Butterfly2.gif")

Then in Timer event you can cycle the images:

Static intCount As Integer
If intCount = 0 Then
   intCount = 1
Else
   intCount = 0
End If
' Use the PaintPicture method to display the bitmaps
'   on the form.
PaintPicture objPics(intCount), 0, 0

By adding a loop to increment the x and y coordinates, you could easily make the butterfly bitmaps "fly" across the form.

Using the Picture Object Instead of the Windows API

There are lots of things you can do with bitmaps, icons, or metafiles in the Windows API, but the Picture object already does most of them for you. This means that you are better off using the Picture object instead of the Windows API whenever possible. The Picture object also allows you to use .jpg and .gif files, whereas the Windows API does not.

There is no direct relationship between a Picture.Handle and a PictureBox.hDC. The hDC property of the picture box is the handle provided by the operating system to the device context of the picture box control. The Handle property of the Picture object is actually the handle of the GDI object that is contained in the Picture object.

There are now two completely different ways to paint graphics on a window (or blit). You can use BitBlt or StretchBlt on the hDC of an object, or you can use the PaintPicture method on the Picture object or property. If you have an Image control, you can only use PaintPicture because Image controls do not have an hDC.

For More Information   For more information about the Windows API, see "Accessing DLLs and the Windows API" in the Component Tools Guide, available in the Professional and Enterprise editions.