问题描述
我正在加载这样的图像:
image = Image.FromFile(//here goes path to image);
比我有像这样的图片框列表
List<PictureBox> pictureBoxes = new List<PictureBox>();
比起我在PictureBox列表中像这样加载图片框
// i is set to one
for (; i < this.images.Count; i++)
{
pictureBoxes.Add((PictureBox)Controls.Find("pictureBox" + i.ToString(),true)[0]);
}
现在我想将该图像加载到pictureBox [0]中。然后,我加载另一个图像,并将其添加到pictureBox [1],依此类推。我尝试这样做超过3天。有人知道怎么做吗?
解决方法
填满图片框列表后,只需找到第一个空白框即可。
var emptyPb = pictureBoxes.Where(x => x.Image == null).FirstOrDefault();
if(emptyPb==null)
{
throw new Exception("No empty picturebox could be found!");
return;
}
emptyPb.Image = images[0];
,
//Set the number of images you have.
int imageCount = 3;
//Create path.
string path = @"C:\ImageFolder\";
//Create the List of PictureBox
List<PictureBox> pictureBoxes = new List<PictureBox>();
//This for will create the name foreach image.
//Example: "pic1","pic2","pic3".
//Assuming that you have the names of each file image like the example.
for (int i = 1; i < imageCount + 1; i++)
{
//Create a PictureBox foreach image
PictureBox pictureBox = new PictureBox()
{
Name = $"pic{i}",//Assign the image file.
Image = Image.FromFile(path + $"pic{i}.jpg")
};
//Add the new pictureBox to the list pictureBoxes
pictureBoxes.Add(pictureBox);
}