Display images
Microsoft .NET Framework, ASP.NET, Visual C# (CSharp, C Sharp, C-Sharp) Developer Training, Visual Studio
| Exam 70-526 Preparation Guide: Display images by using Windows Forms controls |
Contents |
[edit]
PictureBox control
The PictureBox control is an all-around control that displays pictures in several different formats including .bmp, .jpg, .gif, .wmf, and icons. These can be accessed from several locations.
- Hardrive
- Web site
- Resource file
- ImageList
[edit]
Inheritance hierarchy
System.Object
System.MarshalByRefObject
System.ComponentModel.Component
System.Windows.Forms.Control
System.Windows.Forms.PictureBox
[edit]
Useful properties
-
Image- Determines the image that thePictureBoxdisplays.
-
-
ImageLocation- Determines the path for the image to display in thePictureBox.
-
-
InitialImage- vthe image displayed in thePictureBoxcontrol while the main image is loading.
-
-
SizeMode- Indicates how the image is displayed. Takes a value from thePictureBoxSizeModeenumeration:
-
-
AutoSize- ThePictureBoxis sized equal to the size of the image that it contains. -
CenterImage- The image is displayed in the center if thePictureBoxis larger than the image. If the image is larger than thePictureBox, the picture is placed in the center of thePictureBoxand the outside edges are clipped. -
Normal- The image is placed in the upper-left corner of thePictureBox. The image is clipped if it is larger than thePictureBoxit is contained in. -
StretchImage- The image within thePictureBoxis stretched or shrunk to fit the size of thePictureBox. -
Zoom- The size of the image is increased or decreased maintaining the size ratio.
-
[edit]
Useful methods
-
Load(String)- Sets theImageLocationto the specified URL and displays the image indicated.
-
-
LoadAsync(String)- Loads the image at the specified location, asynchronously.
-
[edit]
Load a picture using the designer
- Add a
PictureBoxcontrol to the form. - In the Properties window, select the Image property and click the
button to display the Open dialog.
- If you are looking for a specific file type (for example .jpg files), select it in the Files of type box.
- Select the file you want to display.
[edit]
Clear a picture using the designer
- In the Properties window, select the Image property and right-click the small thumbnail image that appears to the left of the name of the image object.
- Choose Reset.
[edit]
Set a picture manually
You can manually set the image displayed by a PictureBox at run time by setting the Image property using the FromFile method.
Example
You can assume that most computers running the Windows operating system will include a My Documents folder. So to set the Image property to a file named Image.gif, you could use the following code:
private void LoadNewPict() { // Note the escape character used (@) when specifying the path. pictureBox1.Image = Image.FromFile(System.Environment.GetFolderPath (System.Environment.SpecialFolder.Personal) + @"\<b>Image.gif</b>"); }
[edit]
Clear a picture manually
You can manually clear the image being displayed by first releasing the memory being used by the image, and then clearing the graphic. This is shown in the following code:
if (pictureBox1.Image != null) { pictureBox1.Image.Dispose(); pictureBox1.Image = null; }
[edit]
MSDN references
|
© 2007-2008 Mike Kitchen


