C#中Imagelist详解

借由编译器自动生成的代码和动态创建的代码来说一说Imagelist控件的使用:
首先拖入一个imagelist控件,其将在窗口下面分栏显示:

在这里插入图片描述
接着插入一些图片:
在这里插入图片描述
在这里插入图片描述
编译后,我们会发现资源管理器中出现了一个后缀为resx的文件:
在这里插入图片描述
度娘一下得到了如下解释:托管资源, 非代码类的, 图片, 图标, 文件等等, 可以编译进最终的程序集中,
运行时可以用 ResourceManager 来加载使用, 也可以给按钮等控件提供图标, 背景用图。
这玩意一般是系统自己操作生成的,暂且不管。接下来看看系统自己生成的代码:

System.ComponentModel.ComponentResourceManager resources = new System.ComponentModel.ComponentResourceManager(typeof(Form1));
        
		// 
        // imageList1
        // 
        this.imageList1.ImageStream = ((System.Windows.Forms.ImageListStreamer)(resources.GetObject("imageList1.ImageStream")));
        this.imageList1.TransparentColor = System.Drawing.Color.Transparent;
        this.imageList1.Images.SetKeyName(0, "g1.ico");
        this.imageList1.Images.SetKeyName(1, "g2.ico");
        this.imageList1.Images.SetKeyName(2, "g3.ico");
        this.imageList1.Images.SetKeyName(3, "g4.ico");
        this.imageList1.Images.SetKeyName(4, "g5.ico");
        this.imageList1.Images.SetKeyName(5, "g6.ico");
        this.imageList1.Images.SetKeyName(6, "g7.ico");
        this.imageList1.Images.SetKeyName(7, "g8.ico");
        this.imageList1.Images.SetKeyName(8, "g9.ico");
        this.imageList1.Images.SetKeyName(9, "g10.ico");
        this.imageList1.Images.SetKeyName(10, "g11.ico");
        this.imageList1.Images.SetKeyName(11, "g12.ico");
        this.imageList1.Images.SetKeyName(12, "g13.ico");
        this.imageList1.Images.SetKeyName(13, "g14.ico");
        this.imageList1.Images.SetKeyName(14, "g15.ico");
        this.imageList1.Images.SetKeyName(15, "g16.ico");
        this.imageList1.Images.SetKeyName(16, "g17.ico");
        this.imageList1.Images.SetKeyName(17, "g18.ico");
        this.imageList1.Images.SetKeyName(18, "g19.ico");
        this.imageList1.Images.SetKeyName(19, "g20.ico");
        this.imageList1.Images.SetKeyName(20, "g21.ico");
        this.imageList1.Images.SetKeyName(21, "g22.ico");
        this.imageList1.Images.SetKeyName(22, "g23.ico");
        this.imageList1.Images.SetKeyName(23, "g24.ico");
        this.imageList1.Images.SetKeyName(24, "g25.ico");
        this.imageList1.Images.SetKeyName(25, "g26.ico");
        this.imageList1.Images.SetKeyName(26, "g27.ico");
        this.imageList1.Images.SetKeyName(27, "g28.ico");
        this.imageList1.Images.SetKeyName(28, "g29.ico");
        this.imageList1.Images.SetKeyName(29, "g30.ico");
        this.imageList1.Images.SetKeyName(30, "g31.ico");
        this.imageList1.Images.SetKeyName(31, "g32.ico");
        this.imageList1.Images.SetKeyName(32, "g33.ico");
        this.imageList1.Images.SetKeyName(33, "g34.ico");
        this.imageList1.Images.SetKeyName(34, "g35.ico");
        this.imageList1.Images.SetKeyName(35, "g36.ico");
        this.imageList1.Images.SetKeyName(36, "g37.ico");
        this.imageList1.Images.SetKeyName(37, "g38.ico");
        this.imageList1.Images.SetKeyName(38, "g39.ico");
        this.imageList1.Images.SetKeyName(39, "g40.ico");
        this.imageList1.Images.SetKeyName(40, "g41.ico");
        this.imageList1.Images.SetKeyName(41, "g42.ico");
        this.imageList1.Images.SetKeyName(42, "g43.ico");
        this.imageList1.Images.SetKeyName(43, "g44.ico");
        this.imageList1.Images.SetKeyName(44, "g45.ico");
        this.imageList1.Images.SetKeyName(45, "g46.ico");
        this.imageList1.Images.SetKeyName(46, "g47.ico");
        this.imageList1.Images.SetKeyName(47, "g48.ico");
        this.imageList1.Images.SetKeyName(48, "g49.ico");
        this.imageList1.Images.SetKeyName(49, "g50.ico");

正如前面所说的,其新建了一个ResourceManager对象用于加载资源,查询了名为"imageList1.ImageStream"的数据,此时,我们打开前面提到的resx文件,注意使用xml文本编辑器模式打开,找到"imageList1.ImageStream"字段我们看到了如下代码:

  <data name="imageList1.ImageStream" mimetype="application/x-microsoft.net.object.binary.base64">
    <value>
        AAEAAAD/AQAAAAAAAAAMAgAAAFdTeXN0ZW0uV2luZG93cy5Gb3JtcywgVmVyc2lvbj00LjAuMC4w
		//中间太长省略//
        AQEBgAEBAYABAQGAAQEBgAEBAYABAQGAAQEBgAEBAYABAQGAAQEBgAEBAcABAQHAAQEBwAEBAcABAQHA
        AQMBwAEDAcABAwHAAQMB4AEDAeABAwHgAQMB4AEDAfABDwHwAQ8B8AEPAfABDwH8AT8B/AE/AfwBPwH8
        AT8I/ws=
	</value>
  </data>

可见其是以base64编码储存的图片资源,接着使用ImageStream属性加载图像流,ImageListStreamer是图像列表的数据部分。获取完图像,我们需要使用SetKeyName为图像设置键名。至此系统自动生成的代码完成。

那么,当我们需要动态加载图片的时候怎么操作呢,代码如下:


    ImageList ImageList1 = new ImageList();
    ImageList1.ImageSize = new Size(64,64);
    for (int i = 0; i < 50; i++)
    {
        string path =String.Concat("..\\..\\ico\\g" , (i + 1).ToString(),".ico");
        ImageList1.Images.Add(Image.FromFile(path));
        pictureBox1.Image = ImageList1.Images[1];
    }

自行新建一个ImageList实例,规定图标大小64*64,接着使用ImageList.Images.Add方法加入Image对象,这边使用文件路径生成Image对象,至此新建结束。运行这段代码时,我们可以看到并没有生成resx资源文件。
ImageList常用的方法大多在 ImageList.Images下:
方法:
1.ImageList.Images.Add:加入图像
2.ImageList.Images.Clear: 清除图像
3.ImageList.Images.Draw: 绘制指示的图像
4.ImageList.Images.ContainsKey: 确定是否包含指定键的图像
5.ImageList.Images.IndexOfKey:确定包含指定键的图像的序号
6.ImageList.Images.RemoveAt:删除指定序号的图像
7.ImageList.Images.RemoveByKey:删除指定键的图像
8.ImageList.Images.SetKeyName:为指定序号的图像设置键名
属性:
1.ImageList.Images.Count: 集合中包含的图像数
2.ImageList.Images.Empty:集合中是否包含图像

  • 12
    点赞
  • 58
    收藏
    觉得还不错? 一键收藏
  • 2
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值