C# ListView Item Image

前端 未结 2 1334
轻奢々
轻奢々 2021-01-13 04:16

How can i add a image (specified image) into listview with foreach statement for example:

foreach(Video entry in videoFeed.Entries) {

listview1.items.add(en         


        
2条回答
  •  鱼传尺愫
    2021-01-13 04:44

    private void Form1_Load(object sender, EventArgs e)
    {
        List adress = new List()
        {
            "http://i.telegraph.co.uk/multimedia/archive/02351/Jaguar-F-type-9_2351861k.jpg",
            "http://i.telegraph.co.uk/multimedia/archive/02351/Jaguar-F-type-5_2351885k.jpg",
            "http://i.telegraph.co.uk/multimedia/archive/02351/Jaguar-F-type-7_2351893k.jpg"
        };
    
        ImageList il = new ImageList();
    
        DownloadImagesFromWeb(address, il);
    
        il.ImageSize = new Size(32, 32);
        int count = 0;
        listView1.LargeImageList = il;
        List names = new List() { "1", "2", "3", "4" };
    
        foreach (string s in names)
        {
            ListViewItem lst = new ListViewItem();
            lst.Text = s;
            lst.ImageIndex = count++;
            listView1.Items.Add(lst);
        }
    }
    
    private void DownloadImagesFromWeb(List adress, ImageList il)
    {
        foreach (string img in adress)
        {
            System.Net.WebRequest request = System.Net.WebRequest.Create(img);
            System.Net.WebResponse resp = request.GetResponse();
            System.IO.Stream respStream = resp.GetResponseStream();
            Bitmap bmp = new Bitmap(respStream);
            respStream.Dispose();
    
            il.Images.Add(bmp);
        }
    }
    

    This is an option for you not to copy each image manualy to your computer, instead you provide the url and place that image in a new bitmap and add to the list.

提交回复
热议问题