Adding Pictures to Your Application

See Also

Pictures can be displayed in three places in Visual Basic applications:

Pictures can come from paint programs, such as those that ship with the various versions of Microsoft Windows, other graphics applications, or clip-art libraries. Visual Basic provides a large collection of icons you can use as graphics in applications. Visual Basic allows you to add .jpg and .gif files, as well as .bmp, .dib, .ico, .cur, .wmf, and .emf files to your applications. For more information about the graphics formats supported by Visual Basic, see "Using the Image Control" and "Using the Picture Box Control" in "Using Visual Basic's Standard Controls."

You use different techniques to add a picture to a form, a picture box, or an image control depending on whether you add the picture at design time or run time.

Adding a Picture at Design Time

There are two ways to add a picture at design time:

Once you’ve set the Picture property for a form, picture box, or image control — either by loading or pasting a picture — the word displayed in the Settings box is " (Bitmap), " " (Icon), " or " (Metafile). " To change the setting, load or paste another picture. To set the Picture property to " (None) " again, double-click the word displayed in the Settings box and press the DEL key.

Adding a Picture at Run Time

There are four ways to add a picture at run time:

For More Information   For more information about copying a picture from the Clipboard, see "Working with Multiple Formats on the Clipboard."

For information on resource files, see "Working with Resource Files" in "More About Programming."

Note   If you load or paste pictures from files at design time, the pictures are saved and loaded with the form, and the application copies pictures from one object to another. Then, when you create an .exe file, you don’t need to give your users copies of the picture files; the .exe file itself contains the images. Also, consider supplying a .res file and using LoadResPicture. The .res file gets built into the .exe, and the bitmaps are saved in a standard format that any resource editor can read. If you load pictures at run time with the LoadPicture function, you must supply the picture files to your users along with your application.

Removing a Picture at Run Time

You can also use the LoadPicture function to remove a picture at run time without replacing it with another picture. The following statement removes a picture from an image control named imgDisplay:

Set imgDisplay.Picture = LoadPicture("")

Moving and Sizing Pictures

If a form, picture box, or image control is moved (at design time or run time), its picture automatically moves with it. If a form, picture box, or image control is resized so that it is too small to display a picture, the picture gets clipped at the right and bottom. A picture also gets clipped if you load or copy it onto a form or into a picture box or image control that is too small to display all of it.

AutoSize Property

If you want a picture box to automatically expand to accommodate a new picture, set the AutoSize property for the picture box to True. Then when a picture is loaded or copied into the picture box at run time, Visual Basic automatically expands the control down and to the right enough to display all of the picture. If the image you load is larger than the edges of the form, it appears clipped because the form size doesn’t change.

You can also use the AutoSize property to automatically shrink a picture box to reflect the size of a new picture.

Note   Image controls do not have an AutoSize property, but automatically size themselves to fit the picture loaded into them. Forms don’t have an AutoSize property, and they do not automatically enlarge to display all of a picture.

Stretch Property of Image Controls

If you want a picture in an image control to automatically expand to fit a particular size, use the Stretch property. When the Stretch property is False, the image control automatically adjusts its size to fit the picture loaded into it. To resize the picture to fit the image control, set the Stretch property for the image control to True.

Selecting Art for the Picture Control

Where do you get picture files? If you want icons, you can use the Icon Library included with Visual Basic. You can find the icon files within the subdirectories of the main Visual Basic directory (\Vb\Graphics\Icons). You can create .bmp files with Microsoft Paint, or you can buy a clip-art collection that includes bitmap or icon files, or metafiles. You can also create a resource (.res) file containing pictures.

For More Information   See "Working with Resource Files" in "More About Programming, for more information on creating a resource file.