JavaScript Editor JavaScript Editor     JavaScript Debugger

Previous Section Next Section

Main Page

Setting or Getting the Image in a Picture Box

You've added a new picture box to your form, and it looks fine—except for one thing: it's completely blank. How do you add images to a picture box? You use the Image property. You set the Image property to an Image object, which you can create using the Image class's FromFile method. This method is versatile and can load images from bitmap (.bmp), icon (.ico) or metafile (.wmf), JPEG (.jpg), GIF (.gif) files, and other types of files.

At design time, you can click the Image property in the Properties window and click the button with an ellipsis ("") in it to open a dialog box that lets you select an image file to load into a picture box. At run time, you can load an image into the Image property like this, where I'm loading image.jpg (which is on the CD-ROM):

Private Sub Button1_Click(ByVal sender As System.Object, _
    ByVal e As System.EventArgs) Handles Button1.Click
    PictureBox1.Image = _
        Image.FromFile("c:\vbnet\ch07\pictureboxes\image.jpg")
End Sub

You can see this code at work in the PictureBoxes example on the CD-ROM, as you see in Figure 7.16. When you click the "Load image" button, the image you see in the picture box is loaded.

Click To expand
Figure 7.16: Loading an image into a picture box.
Previous Section Next Section




JavaScript Editor Free JavaScript Editor     JavaScript Editor