Using the ImageList Control

Using the ImageList Control

The ImageList control is used to store images that are used by other Windows Forms controls. The ImageList control stores images as if they were stored on a roll of film. Unlike a roll of film, however, it’s a simple matter to add images to and remove images from an ImageList control. Controls such as the ToolBar control (discussed in Chapter 17) use the ImageList control as a repository for the images they display. Other controls that interact with the ImageList control include the ListView, TreeView, and Button controls. (The ListView and TreeView controls are discussed in Chapter 16, and the Button control is discussed in Chapter 12.)

To create an instance of the ImageList control, you’ll typically drag the control onto your form, just as you would any other control. Alternatively, you can create an ImageList object programmatically, by calling its default constructor, as shown here:

ImageList imageList = new ImageList();

The images contained in the ImageList control are accessed through the Images property, which returns a reference to an ImageCollection object, as shown here:

ImageList.ImageCollection collection = imageList.Images;
foreach(Image img in collection)


As with similar collections used with other controls, you’ll rarely need to explicitly employ a reference to the ImageCollection class. Instead, you can use the Images property directly, as shown here:

foreach(Image img in imageList.Images)


Because the ImageCollection class exposes an indexer, you can treat the Images property as an array of Image objects, as shown in the following code:

myButton.BackgroundImage = imageList.Images[0];

Three versions of the Add method are used to add an image to an ImageList control. The first version of the Add method accepts a reference to an Image object, as shown here:

Image anImage = Image.FromFile(@"C:\Windows\winnt256.bmp");

The second version of Add is more advanced, enabling you to specify a masking color for the image, as shown in the following code:

Image img = Image.FromFile(@"C:\Windows\winnt256.bmp");
int index = imageList.Images.Add(img, Color.Black);
pictureBox.Image = imageList.Images[0];

This code causes all the areas that are colored black in the original image to be displayed transparently in the PictureBox control. This version of Add also returns an integer representing the new image’s index position in the ImageList control.

The third version of Add accepts a reference to an Icon object, as shown here:


To remove an image from the ImageList control, call the RemoveAt method, passing the index of the image to be removed, as shown here:


To remove all images from an ImageList control, call the Clear method.


Three of the most commonly used properties exposed by the ImageList class are as follows:

  • ImageSize  A Size value that specifies dimensions for images stored in the ImageList control

  • ColorDepth  A value from the ColorDepth enumeration that specifies the number of bits per pixel allocated for the image color

  • TransparentColor  The color that’s treated as transparent when the image is rendered

The default value for the ImageSize property is 16 by 16 pixels. The Image­Size property will accept any value up to 256 by 256 pixels, as shown here:

imageList.ImageSize = new Size(256, 256);

The ColorDepth property accepts values from the ColorDepth enumeration, listed in Table 15-2.

The following code changes the ImageList control’s color depth to accept true-color (24-bit) images:

imageList.ColorDepth = ColorDepth.Depth24Bit;

Part III: Programming Windows Forms