Compartir a través de


How to: Add or Remove Images with the Windows Forms ImageList Component

The Windows Forms ImageList component is typically populated with images before it is associated with a control. However, you can add and remove images after associating the image list with a control.

NoteNote

When you remove images, verify that the ImageIndex property of any associated controls is still valid.

To add images programmatically

  • Use the Add method of the image list's Images property.

    In the following code example, the path set for the location of the image is the My Documents folder. This location is used because you can assume that most computers that are running the Windows operating system will include this folder. Choosing this location also lets users who have minimal system access levels more safely run the application. The following code example requires that you have a form with an ImageList control already added.

    Public Sub LoadImage()
       Dim myImage As System.Drawing.Image = _
         Image.FromFile _
       (System.Environment.GetFolderPath _
       (System.Environment.SpecialFolder.Personal) _
       & "\Image.gif")
       ImageList1.Images.Add(myImage)
    End Sub
    
    public void addImage()
    {
    // Be sure that you use an appropriate escape sequence (such as the 
    // @) when specifying the location of the file.
       System.Drawing.Image myImage = 
         Image.FromFile
       (System.Environment.GetFolderPath
       (System.Environment.SpecialFolder.Personal)
       + @"\Image.gif");
       imageList1.Images.Add(myImage);
    }
    
    public:
       void addImage()
       {
       // Replace the bold image in the following sample 
       // with your own icon.
       // Be sure that you use an appropriate escape sequence (such as 
       // \\) when specifying the location of the file.
          System::Drawing::Image ^ myImage = 
             Image::FromFile(String::Concat(
             System::Environment::GetFolderPath(
             System::Environment::SpecialFolder::Personal),
             "\\Image.gif"));
          imageList1->Images->Add(myImage);
       }
    

To add images with a key value.

  • Use one of the Add methods of the image list's Images property that takes a key value.

    In the following code example, the path set for the location of the image is the My Documents folder. This location is used because you can assume that most computers that are running the Windows operating system will include this folder. Choosing this location also lets users who have minimal system access levels more safely run the application. The following code example requires that you have a form with an ImageList control already added.

    Public Sub LoadImage()
       Dim myImage As System.Drawing.Image = _
         Image.FromFile _
       (System.Environment.GetFolderPath _
       (System.Environment.SpecialFolder.Personal) _
       & "\Image.gif")
       ImageList1.Images.Add("myPhoto", myImage)
    End Sub
    

To remove all images programmatically

  • Use the Remove method to remove a single image

    ,-or-

    Use the Clear method to clear all images in the image list.

    ' Removes the first image in the image list
    ImageList1.Images.Remove(myImage)
    ' Clears all images in the image list
    ImageList1.Images.Clear()
    

To remove images by key

  • Use the RemoveByKey method to remove a single image by its key.

    ' Removes the image named "myPhoto" from the list.
    ImageList1.Images.RemoveByKey("myPhoto")
    

See Also

Reference

ImageList Component Overview (Windows Forms)

Other Resources

ImageList Component (Windows Forms)
Images, Bitmaps, and Metafiles