Visual Basic Concepts
Using the Image Control
The image control is used to display graphics. Image controls can display graphics in the following formats: bitmap, icon, metafile, enhanced metafile, or as JPEG or GIF files.
Figure 7.27 The image control
In addition, image controls respond to the Click event and can be used as a substitute for command buttons, as items in a toolbar, or to create simple animations.
For More Information See "Lightweight Graphical Controls" in "Forms, Controls, and Menus" for a simple demonstration of using the image control like a command button. See "Creating a Toolbar" in "Creating a User Interface" for information on using image controls to create a toolbar. See "Creating Simple Animation" in "Working with Text and Graphics" for more information on using the image control to create simple animations.
When to Use an Image Control Instead of a Picture Box Control
The image control uses fewer system resources and repaints faster than a picture box control, but it supports only a subset of the picture box control's properties, events, and methods. Both controls support the same picture formats. However, you can stretch pictures in an image control to fit the control's size. You cannot do this with the picture box control.
For More Information See "Cutting Back on Graphics" in "Designing for Performance and Compatibility" for information on using image controls to conserve system resources.
Supported Graphic Formats
The image control can display picture files in any of the following standard formats.
Picture format | Description |
Bitmap | A bitmap defines an image as a pattern of dots (pixels). A bitmap has the file name extensions .gif or .dib. Bitmaps are also called "paint-type" graphics.
You can use bitmaps of various color depths, including 2, 4, 8, 16, 24, and 32-bits, but a bitmap only displays correctly if the display device supports the color depth used by the bitmap. For example, an 8-bit-per-pixel (256 color) bitmap only displays in 16 colors when shown on a 4-bit-per-pixel (16 color) device. |
Icon | An icon is a special kind of bitmap. Icons have a maximum size of 32 pixels by 32 pixels, but under Microsoft Windows 95 or later, icons are also found in 16 by 16 pixel size. An icon has the file name extension .ico. |
Cursor | Cursors, like icons, are essentially bitmaps. Cursors, however, also contain a hot spot, a pixel that tracks the location of the cursor by its x and y coordinates. Cursors have the file name extension .cur. |
Metafile | A metafile defines an image as coded lines and shapes. Conventional metafiles have the file name extension .wmf. Enhanced metafiles have the file name extension .emf. Only files that are compatible with Microsoft Windows can be loaded. Metafiles are also called "draw-type" graphics. |
JPEG | JPEG (Joint Photographic Experts Group) is a compressed bitmap format which supports 8- and 24-bit color. It is a popular file format on the Internet. |
GIF | GIF (Graphic Interchange Format) is a compressed bitmap format originally developed by CompuServe. It supports up to 256 colors and is a popular file format on the Internet. |
Loading a Graphic Into the Image Control
Pictures can be loaded into the image control at design time by selecting the Picture property from the control's Properties window, or at run time by using the Picture property and the LoadPicture function.
Set Image1.Picture = LoadPicture("c:\Windows\Winlogo.cur", vbLPLarge, vbLPColor)
When a picture is loaded into the image control, the control automatically resizes to fit the picture — regardless of how small or large the image control was drawn on the form.
You may want to use icon (.ico) and cursor (.cur) files containing separate images at different sizes and color depths to support a range of display devices. The LoadPicture function's settings allow you to select images with specific color depths and sizes from an .ico or .cur file. In cases where an exact match to the requested settings isn't available, LoadPicture loads the image with the closest match available.
To clear the graphic from the image control, use the LoadPicture function without specifying a file name. For example:
Set Image1.Picture = LoadPicture
This will clear the image control even if a graphic was loaded into the Picture property at design time.
Using the Clipboard
You can also add a graphic to an image control at design time by pasting it from another application. For example, you may want to add a bitmap image that was created in Windows Paint. Simply copy the image to the Clipboard, select the image control, and either use the keyboard shortcut CTRL+V or the Paste command from the Edit menu.
The Stretch Property
The Stretch property determines whether the picture is stretched when the image control is resized at design time. If set to True, the picture loaded into the image control via the Picture property is stretched. Stretching a picture (especially a bitmap format) can produce a loss in image quality, as shown in Figure 7.28. Metafiles, which are "draw-type" graphics, are better suited for stretching.
Figure 7.28 Stretching a bitmap image