简体   繁体   English

C#:将图像路径添加到图像列表

[英]C#: get image path added to the imagelist

An image is added to the imageList1 using this code: 使用以下代码将图像添加到imageList1

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") (在这种情况下:“ 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. 信息:我知道可以使用例如列表来保留引用,我想知道imageList本身的功能。

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. Images属性的类型为ImageCollection,它是Image对象的字典。 The Image object supports the Tag object property in which the programmer can store any information that want to. Image对象支持Tag对象属性,程序员可以在其中存储所需的任何信息。

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;

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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