1.ImageList控件
(1)用途:
用于存储图像资源,并在控件上显示出来。
(2)主要属性:Images
包含关联控件将要使用的图片,每个单独的图像可以通过其索引值或键值来访问。
所有图像以同样的大小显示,大小由ImageSize属性设置。较大的图片将缩小至适当的尺寸。
(3)Images属性的Add方法
用来将指定的图片加到ImageList控件中。
(4)实例部分重要代码
-
private void Form1_Load(object sender, EventArgs e)
-
{
-
-
string Path = "01.jpg";
-
-
string Path2 = "02.jpg";
-
Image Mimg = Image.FromFile(Path, true);
-
imageList1.Images.Add(Mimg);
-
Image Mimg2 = Image.FromFile(Path2, true);
-
imageList1.Images.Add(Mimg2);
-
imageList1.ImageSize = new Size(200, 165);
-
pictureBox1.Width = 200;
-
pictureBox1.Height = 165;
-
}
-
private void button1_Click(object sender, EventArgs e)
-
{
-
-
pictureBox1.Image = imageList1.Images[0];
-
}
-
private void button2_Click(object sender, EventArgs e)
-
{
-
-
pictureBox1.Image = imageList1.Images[1];
-
}
运行截图:

我们知道怎么向ImageList控件里加图片,那怎么移除图像呢?这边注意不是删除原图片哦,只是从ImageList控件里移除而已。
方法一:RemoveAt方法移除单个图像
public void RemoveAt(int index)
参数index表示要移除的图像的索引
方法二:Clear方法清除图像列表中的所有图像
public void Clear()
移除实例代码:
-
private void button1_Click(object sender, EventArgs e)
-
{
-
pictureBox1.Width = 200;
-
pictureBox1.Height = 165;
-
string Path = "01.jpg";
-
Image img = Image.FromFile(Path, true);
-
imageList1.Images.Add(img);
-
imageList1.ImageSize = new Size(200, 165);
-
pictureBox1.Image = imageList1.Images[0];
-
}
-
private void button2_Click(object sender, EventArgs e)
-
{
-
imageList1.Images.RemoveAt(0);
-
pictureBox1.Image = null;
-
}
当然这边只是用的RemoveAt()方法,也可以直接imageList1.Clear()直接移除所有图像。
运行截图:
点击加载图像按钮

点击移除图像按钮
