This documentation is archived and is not being maintained.

Setting the Image Displayed by a Windows Forms Control

Visual Studio .NET 2003

Several Windows Forms controls can display images. This image can be an icon that elucidates the purpose of the control, as in the way a diskette icon on a button traditionally indicates a Save command. Or the icon can be a background image to give the control a certain look and feel. For all image-displaying controls, the image can be set using the Image or BackgroundImage property.

To set the image displayed by a control in the designer

  1. On the Properties window, select the Image or BackgroundImage property of the control, then click the ellipsis button (Aa984419.vbellipsesbutton(en-us,VS.71).gif) to display the Open dialog box.

    If you are looking for a specific file type (for example, .gif files), you can select a category in the Files of type box.

  2. Select the file you want to display.

To set the image displayed by a control programmatically

  • Set the Image or BackgroundImage property to an object of type System.Drawing.Image. Generally you will be loading the image from a file, using the FromFile method of the Image class.

    In the example below, the path set for the location of the image is the My Pictures folder. This is done, because you can assume that most computers running the Windows operating system will include this directory. This also allows users with minimal system access levels to safely run the application. The example below assumes a form with a PictureBox control already added.

    ' Visual Basic
    ' You should replace the bolded image 
    ' in the sample below with an icon of your own choosing.
    PictureBox1.Image = Image.FromFile _
       (System.Environment.GetFolderPath _
       (System.Environment.SpecialFolder.MyPictures) _
       & "\Image.gif")
    
    // C#
    // You should replace the bolded image 
    // in the sample below with an icon of your own choosing.
    // Note the escape character used (@) when specifying the path.
    pictureBox1.Image = Image.FromFile
       (System.Environment.GetFolderPath
       (System.Environment.SpecialFolder.MyPictures)
       + @"\Image.gif");
    
    // C++
    // You should replace the bolded image 
    // in the sample below with an icon of your own choosing.
    pictureBox1->Image = Image::FromFile(String::Concat
       (System::Environment::GetFolderPath
       (System::Environment::SpecialFolder::MyPictures),
       S"\\Image.gif"));
    

See Also

Image.FromFile Method | Image Class | Control.BackgroundImage Property

Show: