Windows应用程序高级控件之ImageList

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()直接移除所有图像。


运行截图:

点击加载图像按钮



点击移除图像按钮



评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值