简体   繁体   中英

C#: get image path added to the imagelist

An image is added to the imageList1 using this code:

imageList1.Images.Add("pic1", Image.FromFile("D:\\pic\\ha-i247.jpg"));

How can we get the full path of each image that added to the imagelist? (in this case: "D:\\pic\\ha-i247.jpg")

INFO: I know that the references can be kept using for example a list, I wondering about the capabilities of imageList itself.

You actually can't do that. Image list doesn't store where image came from. You can put path instead of name:

imageList1.Images.Add("D:\\pic\\ha-i247.jpg", Image.FromFile("D:\\pic\\ha-i247.jpg"));

And if you want to get it from there you can do something like this:

ImageList1.Images.Keys[0].ToString();

But it is not the best solution, better to store it outside the image list

Yes, you can do this. The Images property is of type ImageCollection, which is a dictionary of Image objects. The Image object supports the Tag object property in which the programmer can store any information that want to.

Your code could read;

Image img = Image.FromFile(fileName);
img.Tag = fileName;
imageList.Images.Add('Img1',img);

You can get the file name by using

imageList.Images['Img1'].Tag;

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM