如何使用imageList控件

NIl*_*nke 8 .net c# imagelist winforms

我有一些图像,我手动添加到imageList Cotrol.现在我需要从imageList中删除thart图像,具体取决于键索引并设置为面板背景.

我该怎么办

Nir*_*ngh 16

Images您在Image列表中添加的内容将添加到ImageList.ImageCollection中,因此它是集合类型,然后您可以使用大多数集合方法.

使用Images属性添加,删除和访问要在面板背景中显示的图像. 添加(键,图像)
删除()
RemoveAt()
RemoveByKey()

检查ImageList类文档中的示例,以了解如何实际使用所有这些方法.

添加图片:

imageList1.Images.Add("pic1", Image.FromFile("c:\\mypic.jpg"));
Run Code Online (Sandbox Code Playgroud)

从集合中删除图像:

imageList1.Images.RemoveAt(listBox1.SelectedIndex);
imageList1.Images..RemoveByKey("pic1");
Run Code Online (Sandbox Code Playgroud)

要访问图像,请从imagecollection中获取图像

panel1.BackgroundImage = imageList1.Images[0];
Run Code Online (Sandbox Code Playgroud)

要么

panel1.BackgroundImage = imageList1.Images["pic1"];
Run Code Online (Sandbox Code Playgroud)