Visual Basic Concepts

Adding Pictures to Your Application

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

  • On a form

  • In a picture box

  • In an image control

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 .gif, .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:

  • Load a picture onto a form, or into a picture box or image control from a picture file:

    In the Properties window, select Picture from the Properties list and click the Properties button. Visual Basic displays a dialog box, from which you select a picture file.

    If you set the Picture property for a form, the picture you select is displayed on the form, behind any controls you’ve placed on it. Likewise, if you set the Picture property for a picture box, the picture is displayed in the box, behind any controls you’ve placed on it.

  • Paste a picture onto a form or into a picture box or image control:

    Copy a picture from another application (such as Microsoft Paint) onto the Clipboard. Return to Visual Basic, select the form, picture box, or image control, and from the Edit menu, choose Paste.

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:

  • Use the LoadPicture function to specify a file name and assign the picture to the Picture property.

    The following statement loads the file Cars.gif into a picture box named picDisplay (you name a control by setting its Name property):

    picDisplay.Picture = LoadPicture("C:\Picts\Cars.gif")
    

    You can load a new picture file onto a form or into a picture box or image control whenever you want. Loading a new picture completely replaces the existing picture, although the source files of the pictures are never affected.

  • Use the LoadResPicture function to assign a picture from the project’s .res file into the Picture property.

    The following statement loads the bitmap resource ID, 10, from the resource file into a picture box named picResource:

    Set picResource.Picture = LoadResPicture(10, _
        vbResBitmap)
    
  • Copy a picture from one object to another.

    Once a picture is loaded or pasted onto a form or into a picture box or image control, you can assign it to other forms, picture boxes, or image controls at run time. For example, this statement copies a picture from a picture box named picDisplay to an image control named imgDisplay:

    Set imgDisplay.Picture = picDisplay.Picture
    
  • Copy a picture from the Clipboard object.

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 .gif 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.