簡體   English   中英

如何將圖像加載到 PictureBox 列表

[英]How to load image to PictureBox list

我正在加載這樣的圖像:

image = Image.FromFile(//here goes path to image);

比我有這樣的圖片框列表

List<PictureBox> pictureBoxes = new List<PictureBox>();

比我像這樣在圖片框列表中加載圖片框

// i is set to one
for (; i < this.images.Count; i++)
{
   pictureBoxes.Add((PictureBox)Controls.Find("pictureBox" + i.ToString(), true)[0]);
}

現在我想將該圖像加載到圖片框 [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);
     }

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM