Skip to content

Latest commit

 

History

History
144 lines (119 loc) · 5.8 KB

how-to-add-or-remove-images-with-the-windows-forms-imagelist-component.md

File metadata and controls

144 lines (119 loc) · 5.8 KB
title description ms.date dev_langs helpviewer_keywords ms.assetid
Add or Remove Images with ImageList Component
The Windows Forms ImageList component is typically populated with images before it is associated with a control. Learn to add and remove images later.
03/30/2017
csharp
vb
cpp
images [Windows Forms], removing from ImageList component
images [Windows Forms], storing for controls
ImageList component [Windows Forms], adding images
ImageList component [Windows Forms], removing images
images [Windows Forms], adding to ImageList component
images [Windows Forms], displaying with controls
c5eacc56-f769-4e2e-bfb7-f756620913db

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

The Windows Forms xref:System.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.

Note

When you remove images, verify that the xref:System.Windows.Forms.ButtonBase.ImageIndex%2A property of any associated controls is still valid.

To add images programmatically

  • Use the xref:System.Windows.Forms.ImageList.ImageCollection.Add%2A method of the image list's xref:System.Windows.Forms.ImageList.Images%2A 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 xref:System.Windows.Forms.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 xref:System.Windows.Forms.ImageList.ImageCollection.Add%2A methods of the image list's xref:System.Windows.Forms.ImageList.Images%2A 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 xref:System.Windows.Forms.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  
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("myPhoto", myImage);  
}  

To remove all images programmatically

  • Use the xref:System.Windows.Forms.ImageList.ImageCollection.Remove%2A method to remove a single image

    ,-or-

    Use the xref:System.Windows.Forms.ImageList.ImageCollection.Clear%2A 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()  
// 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 xref:System.Windows.Forms.ImageList.ImageCollection.RemoveByKey%2A method to remove a single image by its key.

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

See also