Sams Teach Yourself Visual Basic 6 in 24 Hours

Sams Teach Yourself Visual Basic 6 in 24 Hours

By Greg Perry

The Image Control

The Image control displays graphics on your Form window. The graphics reside in a file, and the Image control determines how that file's graphic image will appear onscreen.

When you add the Image control to your application's form, you won't see an image of any kind, but rather the outline of a rectangle, as shown in Figure 18.1.

18fig01.gif

Figure 18.1 The Image control doesn't look like much when you first place it.

Preparing the Image

A placed Image control doesn't look like a graphic image until you set appropriate properties. The most important property setting is the Picture property because the Picture property determines which image appears inside the Image control's boundaries on the form. When you click the Picture property, Visual Basic displays an ellipsis button you can click to display a Load Picture dialog box (similar to a File Open dialog box).

The Load Picture dialog box displays a list of files with the graphic-related filename extensions shown in Table 18.1.

Table 18.1. The file types supported by the Image control.

Extension File Description
.bmp A Windows bitmap image file
.cur An animated cursor
.dib An older bitmap image format
.emf An enhanced Windows metafile extension
.gif A Graphic Interchange Format file often used on Web pages
.ico An icon file
.jpg The JPEG image format that stores graphics in a highly compressed format
.wmf A Windows metafile

As long as an image contains one of these filename extensions, you can display that image on your form with the Image control.

You can select a graphic file that you want to load into the Image control's Picture property, and Visual Basic displays that image on the form. If you were to select the Coins.wmf file located in the Graphics\Metafile\Business folder, you would see the coin metafile appear like the one shown in Figure 18.2. In the figure, the BorderStyle property is set to 1-FixedSingle so that you'll know where the Image control edges appear in relation to the image.

18fig02.jpg

Figure 18.2 The Image control enlarges to hold the entire metafile image.

Sizing the Image

If the metafile had been smaller, the Image control would have decreased its size to capture exactly the image's measurements. The Image control shrinks or enlarges to display the entire image. Therefore, the typical sizing properties such as Width and Height don't always mean much when you place an Image control on the form. The Image control will adjust to hold the entire image that you want to display there.

After you place an image on the form, you can resize the Image control just as you can other controls by dragging its sizing handles out and in. Therefore, after you load an image such as the Coins.wmf image, you can adjust the sizing handles to make the Image control smaller.

The Image control's resizing capability can also make the Image control a nuisance. For example, other images and controls might be in place, and an oversized image would overwrite some of their form area. Therefore, you need a way to control the image's size without clipping the image.

To clip or to truncate means to hide part of an image with a control's border.

The f Stretch property controls the Image control's automatic sizing capabilities. When Stretch is False (the default value), the Image control will expand or shrink to display whatever image you load, but the image inside the Image control doesn't change—it's clipped just as described. If you set the Stretch property to True, the image does enlarge or shrink, depending on the size of the Image control. Therefore, if you want to fit an image into a small space, be sure to turn on Stretch before you adjust the Image control's size.

Figure 18.3 shows a form with two Image controls. One is large and one is small, but they both use the same Coins.wmf image you saw earlier. With both controls' Stretch properties set to True, the images themselves grow and shrink inside their boundaries.

18fig03.jpg

Figure 18.3 The images themselves adjust to the Image control borders.

Loading Pictures at Runtime

When your application needs to change the image shown inside an Image control, you cannot simply assign a filename to the Image control's Picture property like this:

imgMyFace.Picture = "C:\Handsome.wmf"  'Not allowed

The Picture property needs more than a simple assignment. To store a new image in the Image control's Picture property, you must use the LoadPicture() built-in function. Here is the syntax of LoadPicture():

LoadPicture([strFile])

strFile is a string literal, variable, or control that contains the complete filename and pathname. The graphic image can reside on another computer that your application computer is networked to. When the application gets to the LoadPicture() function, the graphic image loads and the picture displays.

To load the Handsome.wmf graphic image, you could specify the following line:

imgMyFace.Picture = LoadPicture("C:\Handsome.wmf")  'Allowed

If you want to change the image's size before the image appears, you can set the image's Visible property to False before loading the picture and adjusting the Height and Width properties. Remember to set the Stretch property to True if you want the image to resize and not be clipped. After you adjust the size, you then can set Visible to True, and the image will appear in the size you prefer.

Share ThisShare This

Informit Network