Cutting Back on Graphics

See Also

Graphics (pictures and graphics methods) can consume a lot of memory. To some extent, this is unavoidable: Graphics contain a lot of information, so they tend to be large. But in many cases, you can reduce the impact that graphics have on the size of your application by applying some of the following techniques:

Use the Image Control to Display Bitmaps

The picture controls in many Visual Basic applications exist merely to be clicked or to be dragged and dropped. If this is all you're doing with a picture control, you are wasting a lot of Windows resources. For these purposes, image controls are superior to picture controls. Each picture control is an actual window and uses significant system resources. The image control is a "lightweight" control rather than a window and doesn't use nearly as many resources. In fact, you can typically use five to 10 times as many image controls as picture controls. Moreover, image controls repaint faster than picture controls. Only use a picture controls when you need a feature only it provides, such as dynamic data exchange (DDE), graphics methods, or the ability to contain other controls.

Load Bitmaps from Files As Needed and Share Pictures

When you set a Picture property at design time, you add the picture to the form and thereby increase the memory the form consumes at run time. You can reduce memory consumption by storing pictures in a resource file and using the LoadResPicture function to load them at run time. If you never use all the pictures associated with a form at the same time, this technique saves memory over storing all the pictures in controls on the form. It can speed up form load because not all the pictures need to be loaded before the form can be shown.

You can share the same picture between multiple picture controls, image controls, and forms. If you use code like this you only maintain one copy of the picture:

Picture = LoadPicture("C:\Windows\Chess.bmp")
Image1.Picture = Picture      ' Use the same picture.
Picture1.Picture = Picture   ' Use the same picture.

Contrast that with this code, which causes three copies of the bitmap to be loaded, taking more memory and time:

Picture = LoadPicture("C:\Windows\Chess.bmp")
Image1.Picture = LoadPicture("C:\Windows\Chess.bmp")
Picture1.Picture = LoadPicture("C:\Windows\Chess.bmp")

Similarly, if you load the same picture into several forms or controls at design time, a copy of that picture is saved with each form or control. Instead, you could place the picture in one form and then share it with the other forms and controls as described above. This makes your application both smaller (because it doesn't contain redundant copies of the picture) and faster (because the picture doesn't have to be loaded from disk multiple times).

Use the PaintPicture Method

Rather than placing bitmaps in controls, you can use the PaintPicture method to display bitmaps anywhere on forms. This is particularly useful when you want to tile a bitmap repeatedly across a form: You only need to load the bitmap once, but you can use PaintPicture to draw it multiple times.

Free the Memory Used by Graphics

When you are no longer using a picture in the Picture property of a form, picture box, or image control, set the Picture property to Nothing to empty it:

Set Picture1.Picture = Nothing

If you use the Image property of a picture box or form, Visual Basic creates an AutoRedraw bitmap (even if the AutoRedraw property for that form or picture box is False). When you have finished using the Image property, you can reclaim the memory used by this bitmap by using the Cls method before setting AutoRedraw to False. For example, the following code reclaims the memory used by the Image property for a control called mypic:

mypic.AutoRedraw = True   ' Turn on AutoRedraw bitmap.
mypic.Cls                  ' Clear it.
mypic.AutoRedraw = False      ' Turn off bitmap.

Use Rle-Format Bitmaps or Metafiles

Although the default picture format is the bitmap (.bmp), Visual Basic can also utilize other graphics file formats. Several painting and graphics programs allow you to save bitmaps in a standard compressed bitmap format called Run Length Encoded (.rle). Rle bitmaps can be several times smaller than their uncompressed counterparts, particularly for bitmaps that contain large swatches of solid color, and they aren't appreciably slower to load or display. Using metafiles (.wmf) can produce even greater savings — 10 times or more in some cases. Try to use metafiles at their normal size: They are much slower to paint when they have to be stretched larger or smaller.

You can also use .gif and .jpg formats. They are generally much smaller; however there is some tradeoff in image quality and loading speed.