Windows应用程序高级控件之ImageList

来源:互联网 发布:医院预约管理系统php 编辑:程序博客网 时间:2024/05/01 13:12

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);//创建一个Image对象            imageList1.Images.Add(Mimg);//使用Images属性的Add方法向控件中添加图像            Image Mimg2 = Image.FromFile(Path2, true);//创建一个Image对象            imageList1.Images.Add(Mimg2);//使用Images属性的Add方法向控件中添加图像            imageList1.ImageSize = new Size(200, 165);//设置显示图片的大小            pictureBox1.Width = 200;//设置pictureBox1控件的宽            pictureBox1.Height = 165;//设置pictureBox1控件的高        }        private void button1_Click(object sender, EventArgs e)        {            //设置pictureBox1的图像索引是imageList1控件索引为0的图片            pictureBox1.Image = imageList1.Images[0];        }        private void button2_Click(object sender, EventArgs e)        {            //设置pictureBox1的图像索引是imageList1控件索引为1的图片            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()直接移除所有图像。


运行截图:

点击加载图像按钮



点击移除图像按钮



0 0
原创粉丝点击