Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

C# ListView Item Image

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

foreach(Video entry in videoFeed.Entries) {

listview1.items.add(entry);

listview1.items.image(imageURL);

}
like image 349
Abdülber Kaya Avatar asked Jun 29 '13 15:06

Abdülber Kaya


People also ask

What C is used for?

C programming language is a machine-independent programming language that is mainly used to create many types of applications and operating systems such as Windows, and other complicated programs such as the Oracle database, Git, Python interpreter, and games and is considered a programming foundation in the process of ...

Is C language easy?

Compared to other languages—like Java, PHP, or C#—C is a relatively simple language to learn for anyone just starting to learn computer programming because of its limited number of keywords.

What is C in C language?

What is C? C is a general-purpose programming language created by Dennis Ritchie at the Bell Laboratories in 1972. It is a very popular language, despite being old. C is strongly associated with UNIX, as it was developed to write the UNIX operating system.

What is the full name of C?

In the real sense it has no meaning or full form. It was developed by Dennis Ritchie and Ken Thompson at AT&T bell Lab. First, they used to call it as B language then later they made some improvement into it and renamed it as C and its superscript as C++ which was invented by Dr.


2 Answers

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 ImageList to the ListView and then tell every ListViewItem which image from the list to use:

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 article or in this MSDN tutorial.

like image 170
keenthinker Avatar answered Sep 27 '22 17:09

keenthinker


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.

like image 44
terrybozzio Avatar answered Sep 27 '22 16:09

terrybozzio