如何将图像加载到PictureBox列表

问题描述

我正在加载这样的图像:

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);
     }

相关问答

依赖报错 idea导入项目后依赖报错,解决方案:https://blog....
错误1:代码生成器依赖和mybatis依赖冲突 启动项目时报错如下...
错误1:gradle项目控制台输出为乱码 # 解决方案:https://bl...
错误还原:在查询的过程中,传入的workType为0时,该条件不起...
报错如下,gcc版本太低 ^ server.c:5346:31: 错误:‘struct...