C# ListView 项目图片

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/17381725/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-10 09:23:02  来源:igfitidea点击:

C# ListView Item Image

c#winformslistviewforeach

提问by Abdülber Kaya

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

如何使用 foreach 语句将图像(指定图像)添加到列表视图中,例如:

foreach(Video entry in videoFeed.Entries) {

listview1.items.add(entry);

listview1.items.image(imageURL);

}

采纳答案by pasty

If what you want is to show an image for your ListViewItem, then you need to create an ImageList, fill it with images, assign the ImageListto the ListViewand then tell every ListViewItemwhich image from the list to use:

如果你想要的是显示图像为您ListViewItem的,那么你需要创建一个ImageList中,用图像填充它,将分配的ImageListListView控件,然后告诉每一个ListViewItem其图像从列表中使用:

var listView = new ListView();

// create image list and fill it 
var imageList = new ImageList();
imageList.Images.Add("itemImageKey", image);
// tell your ListView to use the new image list
listView.LargeImageList = imageList;
// add an item
var listViewItem = listView.Items.Add("Item with image");
// and tell the item which image to use
listViewItem.ImageKey = "itemImageKey";

You can read more about ListViewItem and how to set/use images in this MSDN articleor in this MSDN tutorial.

您可以在此MSDN 文章或此MSDN 教程中阅读有关 ListViewItem 以及如何设置/使用图像的更多信息。

回答by terrybozzio

private void Form1_Load(object sender, EventArgs e)
{
    List<string> adress = new List<string>()
    {
        "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<string> names = new List<string>() { "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<string> 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.

这是一个选项,您无需手动将每个图像复制到您的计算机,而是提供 URL 并将该图像放置在新位图中并添加到列表中。