Adding images to the ImageList

In my previous post, I was talking about ListView control, where I have mentioned about the ImageLists. In this one we will try to go deep in to it.

ImageList control is a container of images. Its like a list of images. You can’t see this control on the scren, yet you can make use of this control through other controls. Like for example, in the ListView control, one can add items to the list with some icons to its left. Such a thing is made possible through these ImageLists. This asts like a container of images, which can be associated with any of the controls and used in the application.

As I usually say, the following things can be done the graphical way (using Property windows — F4). Personally I prefer the other way (do it manually !!)

Creating an ImageList

The following statement allows you in the creattion of an ImageList

private System.Windows.Forms.ImageList myImageList = new System.Windows.Forms.ImageList(); //Creates a new instance of an ImageList control for you

Adding images to the ListView control

To add an image to the ListView control, first you need to import the images to the Resource.resx file — Check my other post on how to add images to Resource files in an elaborate fashion [or you can also scroll down for the brief form]

Note – In case you are not able to read or make out anything from my previous post on how to add images to the Resource file, then type the following opening the Resource.resx file by rightclicking the Resources.resx file and by selecting ViewCode option. Then type the following at the end before the root tag,


<data name="foo" type="System.Resources.ResXFileRef" >
<value>..\Resources\1.bmp;System.Drawing.Bitmap</value>
</data>
<data name="bar" type="System.Resources.ResXFileRef" >
<value>..\Resources\2.bmp;System.Drawing.Bitmap</value>
</data>
<data name="baz" type="System.Resources.ResXFileRef" >
<value>..\Resources\3.bmp;System.Drawing.Bitmap</value>
</data>

Where name refers to the object name of the image, type specifies that it will be link in the resource file and value tells the path of the image and the class that can be used to build the image object.

Now considering that you have found a way to add images, whoes names are foo, bar and baz to the resource files, we will now try to add it to the ImageList

Note: Name is different form the file name. Name in here referes to the value of the xnml attribute, name for the data tag. Check your Resources.resx file for more.


myImageList.Images.Add(global::<YourNameSpace>.foo); //Adds the image whoes name (its actually a property) is foo
myImageList.Images.Add(global::<YourNameSpace>.bar);
myImageList.Images.Add(global::<YourNameSpace>.baz);

Using images from the ImageList

To use an image from the ImageList, we can use the Images property of that control.

myPictureBox.Image = myImageList.Images[0]; //Sets the picturebox image as the foo (1.bmp)

Removing images from the ListView control

The following statement will remove an image from the ImageList

myImageList.Images.RemoveAt(0); //Removes the image at the position 0 from the ImageList. If an image has been removed, the list will automatically move its items to fill the deleted portion.

Changing the ImageSize

We can also change the size of the images using the property ImageSize. This is a property of type Size. This can vary between 1 and 256. To specify the size of the images,

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

Make sure that you add this line before making use of the Images from ImageList

There are many other properties to play with the ImageLists. If you find anything interesting, share it with us as a comment to this post.

Tags: , ,

One comment

Leave a comment