Comparing the Picture Box and Image Controls

The picture box control and the image control both do basically the same thing. They allow you to place pictures, from graphics files, on a form. The two controls differ in these respects:

  • The picture box control offers more flexibility by supporting additional methods and properties.
  • The image control is more efficient and works best in sluggish applications on slower PCs.

note

As fast as computers are today, you'll rarely, if ever, be able to tell that the picture box control is less efficient than the image control. Therefore, unless you write for PCs that may be older (as are often found in companies and schools), stick with the picture box control to take advantage of its flexibility.

Both the image control and picture box control support the following graphics file formats:

  • Bitmaps with the .BMP extension
  • Cursors with the .CUR extension
  • Graphics Interchange Format files with the .GIF extension
  • Icons with the .ICO extension
  • JPEG files with the .JPEG or .JPG extension
  • Meta-files with the .WMF or .EMF (for enhanced meta-files) extension
  • Run-length encoded files with the .RLE extension

Several files with these formats appear in the Graphics folder that you installed (or have the option of installing) with Visual Basic.

The most important property of both the image control and the picture box control is the Picture property, which holds the graphic. At design time, you can double-click the Properties window's Picture property to display a File Open dialog box and select a graphics file that has one of the required filename extensions. When you want to display an image at runtime, you must use the LoadPicture() internal function to associate a graphic file's location to the Picture property of the control.

The following assignment associates a graphic to a picture box's Picture property:

picPortrait.Picture = LoadPicture("c:\MyPhotos\Charlie.wmf")

Notice that you don't directly assign the path and file to the Picture property. The LoadPicture() function is the most important function to master when using the image and picture box controls. Here's the full format of the LoadPicture() internal function:

LoadPicture([GraphicFileName] [,varSize] [,varColorDepth], [varX, varY])

Notice that the graphics filename, the first argument of LoadPicture(), is optional. If you call the LoadPicture() function without specifying the filename, Visual Basic will erase the picture from the control.

Table 14.1 lists the named constants you can use for the varSize argument if you specify this argument. The varSize argument specifies the image's size for icons and cursors. The varSize argument is critical because users often use their Control Panel's display settings to determine the size of cursors and icons on their system. You can access these system values.

Table 14.1. Specify one of these varSize constants to control the LoadPicture() image size if you load an icon or cursor file.

Named Constant Value Description
vbLPSmall 0 Small system icon as defined by your video resolution.
vbLPLarge 1 Large system icon as defined by your video resolution.
vbLPSmallShell 2 Determined by the Control Panel's Display Settings page. Click the Appearance tab to see the caption button size to locate the size of images you change with this varSize value.
vbLPLargeShell 3 Determined by the Control Panel's Display Settings page. Click the Appearance tab to see the icon size to locate the size of images you change with this varSize value.
vbLPCustom 4 The varX and varY arguments determine the size.

Table 14.2 lists the values you can use for the optional varColorDepth argument when you place icons and cursors.

Table 14.2. Specify one of these varColorDepth constants to control the LoadPicture() color depth if you load an icon or cursor file.

Named Constant Value Description
vbLPDefault 0 Best match
vbLPMonochrome 1 2 colors
vbLPVGAColor 2 16 colors
vbLPColor 3 256 colors

The varX and varY values are required if you use either the vbLPSmallShell or vbLPLargeShell size values.

When you place image and picture box controls on a form, they respond slightly differently, even if you place them at the same size measurements and point their respective Picture properties to the same graphics file. You must set an image control's Stretch property to True before you set the image control's Width and Height properties. If you do not, the width and height will shrink or expand to the size of the bitmap you place in the control and change the Width and Height settings automatically. When you place a picture box control on a form, the image automatically expands or shrinks to fill your size measurements for the control. Therefore, the picture box control will always change the size of its image to conform to your size property values, but the image control changes your size property values until you set its Stretch property to True.

tip

Not only can you apply the LoadPicture() function to image and picture box controls, but you can apply it to forms as well! Therefore, you can place a graphic on your form's background instead of a solid color. The following statement assigns a check image to the form:

frmCheck.Picture = LoadPicture("Check.wmf")

Figure 14.1 shows the resulting form with command buttons and labels on the form. The picture consumes the form's background. (If you want to place a graphics file on the form's background at design time, use the Properties window's Picture property.)

Figure 14.1

You can place a graphics file as your form's background.