动态设置图片框imagelocation在运行时?动态、图片、imagelocation

2023-09-03 04:38:57 作者:敢愛敢恨的男人才夠範

在这里,我想设置imagelocation是这样的:

  pic1.ImageLocation =;
pic2.ImageLocation =;
 

等等...

 的foreach(ImageResult结果response.Image.Results)
{
    我++;
    图片框thumnailBox =新System.Windows.Forms.PictureBox();
    thumnailBox.Name =峰+ i.ToString();
    //怎么做 ??
    //thumnailBox.ImageLocation = result.Thumbnail.Url;
    //listView1.Items.Add(thumnailBox.Name);
}
 

解决方案

我看你想做什么,你要显示在列表视图每个图像?

更改图片位置 ps如何更改图像窗口排列方式 文件显示的设置 二

您需要添加一个ImageList控件来存储所有的影像,然后将每个ListView项链接到存储的图像。

  //添加图像列表和ListView控件
    ImageList中imglist =新的ImageList(this.components);
    ListView的LST =新的ListView();
    lst.LargeImageList = imglist;
    this.Controls.Add(LST);
 

这是建立你所需要的控制。然后,在你的循环加载图像到图像列表,给它一个键,并使用该密钥在你列表项:

 的for(int i = 0;我℃的;我++)
    {
        imglist.images.add(image.fromfile(最filename.jpg));
        ListViewItem的ITM =新的ListViewItem();
        字符串键=的String.Format(PIC {0},我);
        itm.text =键;
        itm.imagek​​ey =键;
        lst.items.add(ITM);
    }
 

这是伪code,当然它不会编译原样,但你的想法?的

Here i want to set the imagelocation like this:

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

and so on...

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

解决方案

I see what you want to do, you want to show each image in the listview?

You need to add a imagelist control to store all the images, then you link each listview item to the stored image.

    // add imagelist and listview controls
    ImageList imglist = new ImageList( this.components);
    ListView lst = new ListView();
    lst.LargeImageList = imglist;
    this.Controls.Add(lst);

That sets up the controls you need. Then in your loop load the image into the imagelist, give it a key, and use that key in you list item:

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

This is pseudo code, of course it won't compile as is, but you get the idea?