C#中Imagelist控件
一、使用编译器自动生成imagelist控件
1、首先拖入一个imagelist控件,其将在窗口下面分栏显示:
2、插入一些图片:
3、系统自己生成的代码:
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");
}
<span class="token preprocessor property">#<span class="token directive keyword">endregion</span></span>
<span class="token keyword">private</span> <span class="token class-name">System<span class="token punctuation">.</span>Windows<span class="token punctuation">.</span>Forms<span class="token punctuation">.</span>ImageList</span> imageList1<span class="token punctuation">;</span>
}
二、动态创建imagelist控件
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:集合中是否包含图像
本文作者:TwcatL_tree
本文链接:https://www.cnblogs.com/twcat/p/16912598.html
版权声明:本作品采用知识共享署名-非商业性使用-禁止演绎 2.5 中国大陆许可协议进行许可。
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】凌霞软件回馈社区,博客园 & 1Panel & Halo 联合会员上线
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】博客园社区专享云产品让利特惠,阿里云新客6.5折上折
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 微软正式发布.NET 10 Preview 1:开启下一代开发框架新篇章
· 没有源码,如何修改代码逻辑?
· DeepSeek R1 简明指南:架构、训练、本地部署及硬件要求
· NetPad:一个.NET开源、跨平台的C#编辑器
· 面试官:你是如何进行SQL调优的?