Display images

Microsoft .NET Framework, ASP.NET, Visual C# (CSharp, C Sharp, C-Sharp) Developer Training, Visual Studio


Jump to: navigation, search
Exam Prep. Guides
70-505 Study Guide
70-526 Study Guide

1. Forms Controls

2. Integrating Data
3. Printing/Reporting
4. Enhancing Usability
5. Asynchronous Prog.
6. Forms Controls
7. Configure/Deploy

edit
Exam 70-526 Preparation Guide: Display images by using Windows Forms controls


Contents


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.

Image:526_07.jpg


Inheritance hierarchy

System.Object 
  System.MarshalByRefObject 
    System.ComponentModel.Component 
      System.Windows.Forms.Control 
        System.Windows.Forms.PictureBox


Useful properties

  • Image - Determines the image that the PictureBox displays.
  • ImageLocation - Determines the path for the image to display in the PictureBox.
  • InitialImage - vthe image displayed in the PictureBox control while the main image is loading.
  • SizeMode - Indicates how the image is displayed. Takes a value from the PictureBoxSizeMode enumeration:
  • AutoSize - The PictureBox is sized equal to the size of the image that it contains.
  • CenterImage - The image is displayed in the center if the PictureBox is larger than the image. If the image is larger than the PictureBox, the picture is placed in the center of the PictureBox and the outside edges are clipped.
  • Normal - The image is placed in the upper-left corner of the PictureBox. The image is clipped if it is larger than the PictureBox it is contained in.
  • StretchImage - The image within the PictureBox is stretched or shrunk to fit the size of the PictureBox.
  • Zoom - The size of the image is increased or decreased maintaining the size ratio.


Useful methods

  • Load(String) - Sets the ImageLocation to the specified URL and displays the image indicated.
  • LoadAsync(String) - Loads the image at the specified location, asynchronously.


Load a picture using the designer

  1. Add a PictureBox control to the form.
  2. In the Properties window, select the Image property and click the Image:ellipsesbutton.gif button to display the Open dialog.
  3. If you are looking for a specific file type (for example .jpg files), select it in the Files of type box.
  4. Select the file you want to display.


Clear a picture using the designer

  1. 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.
  2. Choose Reset.


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>");
}


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;
}


MSDN references


Previous_Page_.gif Next_Page_.gif
© 2007-2008 Mike Kitchen

Today's Deals: Electronics

Personal tools