Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/.net/24.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 在运行时动态设置picturebox imagelocation?_C#_.net_Winforms_Controls_Picturebox - Fatal编程技术网

C# 在运行时动态设置picturebox imagelocation?

C# 在运行时动态设置picturebox imagelocation?,c#,.net,winforms,controls,picturebox,C#,.net,Winforms,Controls,Picturebox,在这里,我想像这样设置imagelocation: pic1.ImageLocation = ""; pic2.ImageLocation = ""; 等等 foreach (ImageResult result in response.Image.Results) { i++; PictureBox thumnailBox = new System.Windows.Forms.PictureBox(); thumnailBox.Name = "pic" + i.ToS

在这里,我想像这样设置imagelocation:

pic1.ImageLocation = "";
pic2.ImageLocation = "";
等等

foreach (ImageResult result in response.Image.Results)
{
    i++;
    PictureBox thumnailBox = new System.Windows.Forms.PictureBox();
    thumnailBox.Name = "pic" + i.ToString();
    //HOW TO DO ??
    //thumnailBox.ImageLocation = result.Thumbnail.Url;
    //listView1.Items.Add(thumnailBox.Name);                     
}

您的问题是,您可能正在创建一个又一个的所有图片框。您需要设置PictureBox的实际位置(可能还有它们的大小),使它们不会完全或部分重叠

这可以通过代码
thumbnailBox实现。位置=新点(x,y)其中x和y是整数

如前所述,不要忘记更改
缩略图框.Size

更新:另一个问题可能是您需要创建一个PictureBox数组,方法是:

PictureBox[] thumbnailBoxes = new PictureBox[numPics];
并在设置其图像和位置时对其进行迭代


然后,在循环外部,通过执行
Controls.AddRange(thumbnailbox)将整个数组添加到控件中

我知道你想做什么,你想在列表视图中显示每个图像吗

您需要添加imagelist控件来存储所有图像,然后将每个listview项链接到存储的图像

    // add imagelist and listview controls
    ImageList imglist = new ImageList( this.components);
    ListView lst = new ListView();
    lst.LargeImageList = imglist;
    this.Controls.Add(lst);
这将设置您需要的控件。然后在循环中将图像加载到imagelist中,给它一个键,并在列表项中使用该键:

    for (int i = 0; i < 0; i++)
    {
        imglist.images.add(image.fromfile("the-filename.jpg"));
        listviewitem itm = new listviewitem();
        string key = string.format("pic{0}", i);
        itm.text = key;
        itm.imagekey = key;
        lst.items.add(itm);
    }
for(int i=0;i<0;i++)
{
添加(image.fromfile(“文件名.jpg”);
listviewitem itm=新建listviewitem();
string key=string.format(“pic{0}”,i);
itm.text=key;
itm.imagekey=key;
第一项添加(itm);
}

这是伪代码,当然不会按原样编译,但您明白了吗?

有什么问题吗?为什么不
thumnailBox.ImageLocation=result.thumnail.Url为您工作?嗨,科迪·格雷,我想通过其picturebox ID设置ImageLocation属性,例如pic1.ImageLocation=“rose.jpg”;pic2.ImageLocation=“lily.jpg”;图像没有显示吗?你看到错误了吗?您是否调试了代码并查看了response.Image.Results的值?它们是图像的有效URL吗?“你的问题太模糊了,@jack@Wesley我有10张不同的图片,图片显示了,但只显示了一张图片。”。我运行foreach循环以获取所有图像,并设置imagelocation路径“thumnailBox.imagelocation=result.Thumnail.Url”,并将此thumnailBox控件添加到listview1.Controls.add(缩略图);但这只给我看了一张照片。我想在listview中显示所有图片。我希望你能理解我的问题非常感谢韦斯利。这太有帮助了,你解决了我的问题。