C# 如何知道图像类型列表的索引值
本文关键字:列表 索引值 类型 图像 何知道 | 更新日期: 2023-09-27 18:18:20
问题:
我只想获取并显示图像类型列表的索引(数值(。我试图用谷歌搜索它,但大多数列表示例都是 Int 类型。
这是我的代码:注意:我把">?">
foreach (Image currentImage in imageList)
{
MessageBox.Show(String.Format("Image Found({0}) : {1}",?,currentImage.Source));
}
在消息框的"?"上提供什么才能获取数值索引?
我应该得到这种输出显示:
找到的图像 1:"C:''用户''公共''图片''示例图片''图像 1.jpg">
找到的图像 2:"C:''用户''公共''图片''示例图片''图像 2.jpg">
找到的图像 3:"C:''用户''公共''图片''示例图片''图像 3.jpg">
等。。直到列表末尾
使用 for 循环
for (int i=0;i<imageList.Count;++i)
{
MessageBox.Show(String.Format("Image Found({0}):{1}",(i+1),imageList[i].Source));
}
imageList.IndexOf(currentImage(应该可以解决问题。
虽然您可以使用for
循环并索引到列表中,但一个简单的计数器适用于任何IEnumerable
:
int i = 1;
foreach (Image currentImage in imageList)
{
MessageBox.Show(String.Format("Image Found({0}) : {1}",
i++, // this increments i
currentImage.Source));
}