Home > Articles

This chapter is from the book

Using the ImageList Control

The ImageList control is a non-graphical control that is meant to be a container for images. Other .NET Compact Framework controls use the ImageList control to retrieve images they need to display. This is true for the ListView, TreeView, and ToolBar controls. The usage of the ImageList with these controls is discussed in their respective sections.

Images can be added to the control at design time as well as at runtime. To add an ImageControl to a Form, drag an ImageControl from the ToolBox to the application form in the Form Designer. Now click the ellipsis next to the Images property in the Properties window. This will bring up the Image Collection Editor (see Figure 3.17). Use the Image Collection Editor to add the images to the ImageList. It is important to note that the

images will be resized to 16 x 16 pixels by default. You can set the ImageSize property if you want the images to be resized differently. These images also get imported into the resource file for the application. You need not deploy the images along with the application, because they will be embedded inside the application assembly.

Images can also be loaded at runtime. The ImageList control exposes an Images property, which represents the list of images stored in the control. The Images property provides the Add method, which allows you to add images to the list of images. Interestingly, this method will add the image to the head of the list (index 0)—not the tail of the list, like other methods that add objects to collections would. The Add method can accept either a System.Drawing.Icon or a System.Drawing.Image. Images loaded at runtime can be either loaded from the file system or loaded from the assembly's resource by using the ResourceManager. The following code demonstrates how to load an image from the assembly's resource file:

BitMap image = 
 new BitMap(Assembly.GetExecutingAssembly().GetManifestResourceStream("image1.jpg");
ImageList imgList = new ImageList();
Dim imgLst as new ImageList()
Dim image as BitMap
image = new _ BitMap(Assembly.GetExecutingAssembly().GetManifestResourceStream("image1.jpg")
  • + Share This
  • 🔖 Save To Your Account