Graphics Controls

You din place graphics on three controls:

• Form
• PictureBox
• ImageBox

The main difference in these three controls is that the ImageBox control is designed specifically for displaying images and not for creating new images or manipulating them. The other two controls provide drawing methods that let you design graphics at runtime.
The methods for loading graphics on the various controls are simpler than creating graphics from scratch. You can place graphics oncontrols-at design time and , runtime. To load a graphic (bitmap or icon) on a control at design time, you assign its filename to the Picture property of the control in the Properties window. This same procedure can’t change the image displayed at runtime; instead, you must use the LoadPictureO function, which is described shortly.
If the graphic is assigned to a control at design time, ‘it’s stored along with the application. One of the files that the Visual Basic editor generates for each Form in a project has the extension FRX.This is where the image (the actual bitmap) is stored. As a consequence, the size of the application will increase if its Forms contain bitmaps that must be loaded at runtime. The alternative is to load the .graphic at runtime with the LoadPictureO function. This reduces the size of the fRX file, but your application must make sure the file is available at runtime. Vector drawings aren’t loaded, they are generated on the fly (we’ll explore the drawing commands in the second part of the chapter).

Sizing Image

When an linage is loaded on a Form or a PictureBox, you must make sure it will fill the available space, unless you let the user select the graphic at runtime. Graphics are usually placed on ImageBox or Picture Box controls. The lmageBox is good for displaying graphics and uses fewer resources than PictureBox controls, The Picture ” Box control provides methods for drawing at runtime and is much more flexible than the Image.Box control. As a consequence, the PictureBox control uses more resources. Each control provides a different property for controlling the appearance of the picture displayed

The ImageBox ,Control’

If the Stretch property is True, the image is resized to fill the area of the ImageBox control. Unless the ~ontrol’s dimensions have the same aspect tatio as the image’s dimensions, the image is distorted as it’s resized. If the Stretch property is False, the ImageBox control behaves like a PictureBox with its Aut6Size property set to True. Figure 6.3 shows two ImageBox controls, both containing the same linage. Notice how the image is resized in the ImageBox control whose Stretch property is True.

The PictureBox Control

If the AutoSize property is True, the control is resized to the dimensions of the image it contains. If the AutoSize property is False, only the part of the image that can fit on the control is displayed.

Loading and Saving Images

To load a graphic on a control at runtime, use the LoadPicture method as follows:

Forml. Picture – LoadPicture (fileNam)”

The fileName variable is ‘the name of the file containing the graphic. This file can have one of the following ‘extensions:

• BMP             Bitmap
• GIF               Graphics Interchange Format
• JPG             Joipt Photographic Experts Group .
• DIB              Device Independent Bitmap
• WMF            Windows MetaFile
• EMF             Enhanced MetaFile
• reo               Icons

At present, Visual Basicdoesn’t support other types of graphic files.

If you use the LoadPicture method without an argument, the current picture is unloaded (along with any drawing or printing painted onto the control or Form with the Circle, Line, PSet, or Print commands). Calling the LoadPicture without an argument clears the control. To remove a graphic from a Picture Box (or any control that’ can play graphics), issue the following command: Picturel. Picture LoadPicture() This technique is similar to the Cls method, which clears any drawing-or printing from a Form or a control.

If your application processes the displayed image during the course of its execution and you want to save the image, you can use the SavePicture statement. Its syntax is as follows:

SavePicture picture, filename

The picture argument is the Picture property of the PictureBox or ImageBox control whose contents you want to save, Hndfilenanu: is the name of thefile that will store the image.
To save the contents of the Picturel control to file, you must use a statement like the following:

SavePicture Picturel. Picture,  c:/ tmplma·Je .bmp

The Savef’icture statement supports only BMP files, even if the original image loaded on the control comes from a GlF or a JPG file. When prompting the user w.ith the FileOpen common dialog box to select an image file to open, Y9U can use the extensions aMP, GIF, or JPG. In the corresponding FileSave common dialog box though, you can specify only the I3MP(or DIB) extension .

Scroll to Top