Section 13.8. PictureBoxes


13.8. PictureBoxes

A PictureBox displays an image. The image can be one of several formats, such as bitmap, GIF (Graphics Interchange Format) and JPEG. (Images are discussed in Chapter 17, Graphics and Multimedia.) A PictureBox's Image property specifies the image that is displayed, and the SizeMode property indicates how the image is displayed (Normal, StretchImage, Autosize or CenterImage). Figure 13.29 describes common PictureBox properties and a common event.

Figure 13.29. PictureBox properties and an event.

PictureBox properties and an event

Description

Common Properties

Image

Sets the image to display in the PictureBox.

SizeMode

Enumeration that controls image sizing and positioning. Values are Normal (default), StretchImage, AutoSize and CenterImage. Normal places the image in the top-left corner of the PictureBox; CenterImage puts the image in the middle. Both options truncate the image if it is too large. StretchImage resizes the image to fit in the PictureBox. AutoSize resizes the PictureBox to fit the image.

Common Event

Click

Occurs when the user clicks the control. Double clicking this control design mode generates an empty event handler for this event.


Figure 13.30 uses a PictureBox named picImage to display one of three bitmap images image0, image1 or image2. These images are located in the project's bin/Debug and bin/Release directories in the subdirectory images. Whenever a user clicks the Next Image Button, the image changes to the next image in sequence. When the last image is displayed and the user clicks the Next Image Button, the first image is displayed again. Event handler btnNext_Click (lines 1017) uses Integer variable imageNum to store the number of the image we want to display. We then set the Image property of picImage to an Image (lines 1516).

Figure 13.30. Using a PictureBox to display images.

  1  ' Fig. 13.30: FrmPictureBoxTest.vb  2  ' Using a PictureBox to display images.  3  Imports System.IO  4  5  Public Class FrmPictureBoxTest  6     ' determines which image is displayed  7     Private imageNum As Integer = -1  8  9     ' change image whenever Next Button is clicked 10     Private Sub btnNext_Click(ByVal sender As System.Object, _ 11        ByVal e As System.EventArgs) Handles btnNext.Click 12        imageNum = (imageNum + 1 ) Mod 3 ' imageNum cycles from 0 to 2 13 14        ' create Image object from file, display in PicutreBox              15        picImage.Image = Image.FromFile(Directory.GetCurrentDirectory() & _ 16           "\images\image" & imageNum & ".bmp")                                17     End Sub ' btnNext_Click 18  End Class ' FrmPictureBoxTest 

(a)

(b)

(c)



Visual BasicR 2005 for Programmers. DeitelR Developer Series
Visual Basic 2005 for Programmers (2nd Edition)
ISBN: 013225140X
EAN: 2147483647
Year: 2004
Pages: 435

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net