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 |
|
|
c5eacc56-f769-4e2e-bfb7-f756620913db |
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.
-
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); }
-
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);
}
-
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();
-
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");