如何解决如何将图像加载到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);
}
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。