一、使用编译器自动生成imagelist控件
1、首先拖入一个imagelist控件,其将在窗口下面分栏显示:
2、插入一些图片:
3、系统自己生成的代码:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 | private void InitializeComponent() { this.components = new System.ComponentModel.Container(); System.ComponentModel.ComponentResourceManager resources = new System.ComponentModel.ComponentResourceManager(typeof(Form1)); this.imageList1 = new System.Windows.Forms.ImageList(this.components); this.SuspendLayout(); // // imageList1 // this.imageList1.ImageStream = ((System.Windows.Forms.ImageListStreamer)(resources.GetObject("imageList1.ImageStream"))); this.imageList1.TransparentColor = System.Drawing.Color.Transparent; this.imageList1.Images.SetKeyName(0, "10000.png"); this.imageList1.Images.SetKeyName(1, "10001.png"); this.imageList1.Images.SetKeyName(2, "10002.png"); this.imageList1.Images.SetKeyName(3, "10003.png"); this.imageList1.Images.SetKeyName(4, "10004.png"); this.imageList1.Images.SetKeyName(5, "10005.png"); this.imageList1.Images.SetKeyName(6, "10006.png"); this.imageList1.Images.SetKeyName(7, "10007.png"); this.imageList1.Images.SetKeyName(8, "10008.png"); this.imageList1.Images.SetKeyName(9, "10009.png"); this.imageList1.Images.SetKeyName(10, "10010.png"); this.imageList1.Images.SetKeyName(11, "10011.png"); this.imageList1.Images.SetKeyName(12, "10012.png"); this.imageList1.Images.SetKeyName(13, "10013.png"); this.imageList1.Images.SetKeyName(14, "10014.png"); this.imageList1.Images.SetKeyName(15, "10015.png"); this.imageList1.Images.SetKeyName(16, "10016.png"); this.imageList1.Images.SetKeyName(17, "10017.png"); this.imageList1.Images.SetKeyName(18, "10018.png"); this.imageList1.Images.SetKeyName(19, "10019.png"); this.imageList1.Images.SetKeyName(20, "10020.png"); this.imageList1.Images.SetKeyName(21, "10021.png"); this.imageList1.Images.SetKeyName(22, "10022.png"); this.imageList1.Images.SetKeyName(23, "10023.png"); this.imageList1.Images.SetKeyName(24, "10024.png"); this.imageList1.Images.SetKeyName(25, "10025.png"); this.imageList1.Images.SetKeyName(26, "10026.png"); this.imageList1.Images.SetKeyName(27, "10027.png"); this.imageList1.Images.SetKeyName(28, "10028.png"); this.imageList1.Images.SetKeyName(29, "10029.png"); this.imageList1.Images.SetKeyName(30, "10030.png"); this.imageList1.Images.SetKeyName(31, "10031.png"); } #endregion private System.Windows.Forms.ImageList imageList1; } |
二、动态创建imagelist控件
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 | ImageList ImageList1 = new ImageList(); ImageList1.ImageSize = new Size(50,50); //声明一个字符串,用来保存图片路径 string path; for (int i = 0; i < 32; i++) { if (i<10) { path = String.Concat("../../images/Bomb/1000", i.ToString(), ".png"); } else { path = String.Concat("../../images/Bomb/100", i.ToString(), ".png"); } imageList.Images.Add(Image.FromFile(path)); } |
用代码创建一个ImageList实例,规定图标大小50×50,接着使用ImageList.Images.Add方法加入Image对象,这边使用文件路径生成Image对象。
三、ImageList常用的方法大多在 ImageList.Images下
方法:
- ImageList.Images.Add:加入图像
- ImageList.Images.Clear: 清除图像
- ImageList.Images.Draw: 绘制指示的图像
- ImageList.Images.ContainsKey: 确定是否包含指定键的图像
- ImageList.Images.IndexOfKey:确定包含指定键的图像的序号
- ImageList.Images.RemoveAt:删除指定序号的图像
- ImageList.Images.RemoveByKey:删除指定键的图像
- ImageList.Images.SetKeyName:为指定序号的图像设置键名
属性:
- ImageList.Images.Count: 集合中包含的图像数
- ImageList.Images.Empty:集合中是否包含图像