WindowForm组件ImageList

ImageList组件
图片集合 用于存储图像资源,并在关联控件中显示出来
指定关联控件的ImagesList属性
属性
Images 图像集合 同样大小的尺寸显示 每张图片 索引、图片名
ImageSize图片大小 Name

Images方法 Images 都是动态添加
Add(Image/Icon) 添加图片
Add(string,Image/Icon) 键值,图片
Contains(Image/String) 判断是否存在(键值/图片)
IndexOf(Image/String) 获取索引(键值/图片)
Remove(Image) 移除(图片)
RemoveAt(int) 移除(索引)
RemoveByKey(string) 移除(键值)
SetKeyName(int,string) 设置索引位置的键值

Images 代码添加图片

private void FrmImageList_Load(object sender, EventArgs e)
{
    
    
    string path = @"D:\课件\WF\WFFormUse\WFFormUse\Imgs";		//路径
    if(Directory.Exists(path))  					//路径存在性判断
    {
    
    
        string[] files = Directory.GetFiles(path);			//取到指定目录下全路径文件名
        if(files.Length>0)
        {
    
    
            string[] fileType = {
    
     ".jpg", ".png" };
            foreach (string fpath in files)
            {
    
    
                if(fileType.Contains(Path.GetExtension(fpath)))		//指定格式
                {
    
    
                    //添加到Images
                    Image img = Image.FromFile(fpath);
                    string keyName= Path.GetFileNameWithoutExtension(fpath);
                    imgList.Images.Add(keyName, img);			//添加(键值,图片)
                }
            }
            imgList.ImageSize = new Size(30, 30);			//图片大小
            //label1.ImageIndex = 2;   					//图像集合 索引(0,1,2,...)
            label1.ImageKey = "03-hu"; 					//图像集合 键值
        }
    }
}

猜你喜欢

转载自blog.csdn.net/asdasd1fdsyrt/article/details/109480628
今日推荐