Picture Box Control

The PictureBox control is the basic control used for displaying images in the user interface. The PictureBox control can display pictures in a variety of formats, including .bmp, .jpg, .gif, metafiles, and icons. You can display images that are present in application resource files or compiled into the application, or you can load images from a Web or disk address. Table 3-15 details important properties of the PictureBox control.

Table 3-15 Important Properties of the PictureBox Control

Property

Description

ErrorImage

The image that will be displayed if the selected image fails to load.

Image

The image to be loaded in the PictureBox.

ImageLocation

A Web or disk address to load the image from.

Initiallmage

The image to be displayed in the PictureBox while the image is loading.

SizeMode

Determines how the control handles image placement and sizing.

You can set the Image property at design time by clicking it in the Properties window, which opens the Select Resource dialog box, shown in Figure 3-10.

Select Resource

Select Resource

Figure 3-10 The Select Resource dialog box

Figure 3-10 The Select Resource dialog box

You can select an image resource that is already present in a project resource file by selecting the Project Resource File radio button and selecting the .resx file that contains the image, or you can import a new image into a resource file by clicking the Import button and navigating to the image you want to import. The selected image will be added to the selected .resx file. You can also import the image as a local resource by selecting the Local Resource radio button and clicking the Import button to browse to the image you want to import. Importing an image as a local resource will make it available only to the PictureBox control and unavailable to the rest of the application.

Instead of loading an image from a resource, you can specify a URL from which to load an image by setting the ImageLocation property. When the ImageLocation property is set, the image is loaded from the specified address, and the Image property is set to that image.

At run time, you can set the Image property to an instance of an image, as shown in the following example:

Dim anImage As New System.Drawing.Bitmap("C:\anImage.bmp") PictureBox1.Image = anImage

System.Drawing.Bitmap anImage = new

System.Drawing.Bitmap(@"C:\anImage.bmp"); pictureBox1.Image = anImage;

Was this article helpful?

0 0

Post a comment