我正在尝试读取当前图像文件名或图像名称,例如0.png或1.png,但是失败了。我正在尝试如下以及更多。在下面的代码中,images
是已包含
images.Add(Image.FromFile(fileinfo.FullName));
任何建议或想法都会有所帮助。是否可以将文件名存储在我程序的当前位置?
foreach (Control control in tableLayoutPanel1.Controls) // run loop untill every control inside tablelayoutpanel1.Controls is check or read
{
Button btns = control as Button; // btn sotre the current button in table.contr
if (btns != null) // check btn got a button from the panel then
{
int randomNumber = random.Next(images.Count); // pic the random image from list by its index which are already in images list and also count how many are they
//MessageBox.Show(images.Count.ToString()); // if 6 images then 6 will be the max index value for
btns.BackgroundImage = images[randomNumber]; // change btn image to current random image
copyImages.Add(images[randomNumber]); // fileName.Add(** here some query for storing current image file name required**);
btns.BackgroundImage = null;
images.RemoveAt(randomNumber); // remove the current image index from list
//btns.Hide();
}
}
fileName
是我创建的列表。我只想将文件名存储在其0索引上,或者我可以做类似的设置图像:将图像的名称存储在Button的Tag属性中?如果是,怎么样?
答案 0 :(得分:0)
您正在为按钮分配图像,然后将其删除:
btns.BackgroundImage = images[randomNumber];
...
btns.BackgroundImage = null;
这不可能是正确的。
答案 1 :(得分:0)
除了Steve Wellens发现的虫子之外 有几种方法可以做到这一点。 最简单的是
将图片设为List<String>
个文件名。
然后在循环中调用From File
e.g
btns.Tag = images[randomnumber];
btns.BackgroundImage = ImageFromFile(images[randomNumber]);
如果您有一些深层次需要在使用之前加载所有图像。 然后其他选项将是结构或类(FileName和Image),并有一个列表。
如果文件名是唯一的,请将图片设为Dictionary<string,Image>
,其中字符串是文件名,并使用Keys.Count
和Keys.ElementAt
方法使您的循环正常工作。