WindowForm component ImageList

ImageList component The
image collection is used to store image resources and display them in the associated control.
Specify the ImagesList property of the associated control.
Attribute
Images. The same size of the image collection. Display the index and name of each image.
ImageSize image size Name

Images method Images are dynamically added
Add(Image/Icon) Add image
Add(string,Image/Icon) key value, image
Contains(Image/String) judge whether it exists (key value/picture)
IndexOf(Image/String) Get index (Key value/picture)
Remove(Image) Remove(image)
RemoveAt(int) Remove(index)
RemoveByKey(string) Remove(key value)
SetKeyName(int,string) Set the key value of the index position

Images code to add pictures

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"; 					//图像集合 键值
        }
    }
}

Guess you like

Origin blog.csdn.net/asdasd1fdsyrt/article/details/109480628