Using a Picture Box to View Images

So far in our sample applications, we have used a form as the drawing surface for images. You can also use a PictureBox control to view images. Picture boxes are easy to use, and this control optimizes the rendering process with a built-in double buffering feature. A picture box is recommended for viewing (but not manipulating) images when you know the exact size of the image.

The PictureBox class is defined in the System.Windows.Forms namespace. The Image property, which takes an Image object, sets the image to the picture box that you want to display. You can also set the position and clipping, using the SizeMode property. SizeMode, which is of type PictureBoxSizeMode enumeration, specifies how an image is positioned within a picture box. The members of the PictureBoxSizeMode enumeration are defined in Table 7.6.

To view an image in a PictureBox control, we simply create an Image object using any of the Image class methods and set the PictureBox.Image property to that image.

Listing 7.27 views an image in a picture box. To test this code, create a Windows application, add a PictureBox control to the form by dragging it from the toolbox, and add code to the form-load event handler.

Listing 7.27 Viewing an image in a picture box

Image curImage = Image.FromFile("roses.jpg");
pictureBox1.Image = curImage;
pictureBox1.SizeMode = PictureBoxSizeMode.StretchImage;

Figure 7.41 shows the output from Listing 7.27.

Figure 7.41. Viewing an image in a picture box

graphics/07fig41.jpg

Table 7.6. PictureBoxSizeMode members

Member

Description

AutoSize

The picture box is automatically set to the same size as the image.

CenterImage

The image is displayed in the center of the picture box.

Normal

The image is placed in the upper left corner of the picture box and clipped if it is larger than the control.

StretchImage

The image is stretched or shrunk to fit the size of the picture box.

GDI+: The Next-Generation Graphics Interface

Your First GDI+ Application

The Graphics Class

Working with Brushes and Pens

Colors, Fonts, and Text

Rectangles and Regions

Working with Images

Advanced Imaging

Advanced 2D Graphics

Transformation

Printing

Developing GDI+ Web Applications

GDI+ Best Practices and Performance Techniques

GDI Interoperability

Miscellaneous GDI+ Examples

Appendix A. Exception Handling in .NET



GDI+ Programming with C#
GDI+ Programming with C#
ISBN: 073561265X
EAN: N/A
Year: 2003
Pages: 145

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