If you want to do this in the designer, you can take the following steps to add the
images to the ListView control:
- Switch to the designer, click on the ImageList component on the Component Tray,
there will be a smart tag appear on the top-right corner of the ImageList.
- Click the smart tag, and click "Choose Images" on the pane.
- On the pop-up Image Collection Editor dialog, choose the images from the folder
your want.
- Click OK to finish adding images to the ImageList.
- Click the ListView on the form, there will be a smart tag appear on the top-right
corner.
- Click the smart tag, you will find there're three ComboBoxes there, choose a
ImageList from the list as you want.
- Click the "Add items" option on the smart tag, a ListViewItem Collection Editor
will appear, you can add items to the ListView, it's important here to set the
ImageIndex or ImageKey property, or the image won't appear.
- Click OK to finish item editing, now you'll find the images are displayed on the
ListView.
If you want to add the images to the ListView by code, you can do something like this`
Code Snippet
private void Form10_Load(object sender, EventArgs e)
{
DirectoryInfo dir = new DirectoryInfo(@"c:\pic");
foreach (FileInfo file in dir.GetFiles())
{
try
{
this.imageList1.Images.Add(Image.FromFile(file.FullName));
}
catch{
Console.WriteLine("This is not an image file");
}
}
this.listView1.View = View.LargeIcon;
this.imageList1.ImageSize = new Size(32, 32);
this.listView1.LargeImageList = this.imageList1;
//or
//this.listView1.View = View.SmallIcon;
//this.listView1.SmallImageList = this.imageList1;
for (int j = 0; j < this.imageList1.Images.Count; j++)
{
ListViewItem item = new ListViewItem();
item.ImageIndex = j;
this.listView1.Items.Add(item);
}
}
Source