ImageList
This control provides a container for image data. It is referenced from other controls such as ListView
, which get the images from index values into the ImageList
.
Please add an ImageList
control by double-clicking on the ImageList
entry in the Toolbox. It will appear in the tray of the designer at the bottom.
You can add any image to the ImageList
dynamically by invoking the Add method. This method has several overloaded versions and you can call any of them.
Image.FromFile
method to read the data.Form1_Load
event handler is used to make sure the code is run at startup of the application.using System; using System.Drawing; using System.Windows.Forms; namespace WindowsFormsApplication1 { public partial class Form1 : Form { public Form1() { InitializeComponent(); } private void Form1_Load(object sender, EventArgs e) { // Add these file names to the ImageList on load. string[] files = { "image.png", "logo.jpg" }; var images = imageList1.Images; foreach (string file in files) { // Use Image.FromFile to load the file. images.Add(Image.FromFile(file)); } } } }
ListView
How can we reference an ImageList
in other controls such as the ListView
? On the ListView
, find the LargeImageList
and SmallImageList
properties.
ImageList
instance you created as the value of these properties.ListView
to specify the index of the images inside your ImageList
.By providing a container for images, the ImageList
control gives Windows Forms programs another level of abstraction for managing images.