0

I am loading images like this:

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

Than i have list of pictureBoxes like this

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

Than i load picture boxes in pictureBox list like this

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

And now I want to load that image in pictureBox[0]. Then I load another image and I want to add it to pictureBox[1] and so on. I am trying to do this more than 3 days. Does anyone know how to do it?

Sh.Imran
  • 1,035
  • 7
  • 13
  • Would setting [PictureBox.Image](https://learn.microsoft.com/en-us/dotnet/api/system.windows.forms.picturebox.image?view=netcore-3.1#System_Windows_Forms_PictureBox_Image) help? – Sebastián Aug 16 '20 at 20:00
  • `pictureBoxes[0].Image = Image.FromFile(@"c:\img.png")` -- is that what you are trying to do? I may not be understanding your question. – Andy Aug 16 '20 at 22:26
  • Yes i want that but a i have 6 pictireBoxes in List and i somehow want to add image to first pictureBox thats Empty. So lets say that i have 6 of them empty. And i do pictureBoxes[0].Image = Image.FromFile(@"c:\img.png"). Now i load another image and i have to add id to next free pictureBox, so it would just be at index 1 and than 2 and so on. But my problem is that i have to load image and then add it to pictureBox. Is there any method that says "add image to first empty pictureBox"? – Aleksa Mitic-Smile Aug 17 '20 at 06:09

2 Answers2

0

After you filled your picturebox list, just find the first empty one.

  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];
SerhatDev
  • 241
  • 1
  • 4
-1
     //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);
     }
lozadakb
  • 184
  • 1
  • 2
  • 13