C#:.NET:Windows

.NET Array Dictionary List String 2D Async DataTable Dates DateTime Enum File For Foreach Format IEnumerable If IndexOf Lambda Parse Path Process Property Regex Replace Row Sort Split Static StringBuilder Substring Switch Tuple Window

ImageList provides a container for image data. The control is not visible directly. It is instead referenced from other controls such as ListView, which acquire the images from index values into the ImageList.

Info: The ImageList is simple to create. We add images manually or dynamically.



Add ImageList. To begin, let's add an ImageList control to your Windows Forms program in Visual Studio by double-clicking on the ImageList entry in the Toolbox. The ImageList will appear in the tray of the designer at the bottom.

Next: Try right-clicking on the ImageList instance and select Properties. From there, you can add images manually in the dialog.


ListView. How can you reference the ImageList in other controls such as the ListView? First, create the ImageList. Then, find the LargeImageList and SmallImageList properties on the ListView.

Instance: Please select the ImageList instance you created as the value of these properties.

Then: You can use the ListView to specify the index of the images inside your ImageList.

ListView
Add Images. 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. Here we look at code that uses the Add method.
In this example, we have a list of file names, and then add each as an Image object using the Image.FromFile method to read the data. The Form1_Load event handler is used to make sure the code is run at startup of the application.
Example of using Add method on ImageList: C#

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));
	    }
	}
    }
}
Summary. By providing a container for images, the ImageList control gives Windows Forms programs another level of abstraction for managing images. You can reuse the same image in multiple places or with multiple controls.

Finally: You can add images manually through Visual Studio or dynamically through C# code using the ImageList.