Missing class members ImageList in .NETCF

Recently I was trying to enhance CHMReader (http://code.google.com/p/chmreader-smartphone/) and wanted to use an image against CHM files which were shown in the FileBrowser dialog. The project was compiled for Windows Mobile 5 & .NETCF 2.0. I upgraded it to Windows Mobile 6 and .NETCF 3.0.  It is using a TreeNode class (http://msdn.microsoft.com/en-us/library/system.windows.forms.treenode.aspx) which has the possibility to show such an image besides the name by using the ImageIndex property. This ImageIndex comes from an associated ImageList (http://msdn.microsoft.com/en-us/library/system.windows.forms.imagelist.aspx). The problem with .NETCF is that some of the (easier) members are not available. To add an image to this list, we need to use the Image Class (http://msdn.microsoft.com/en-us/library/system.drawing.image.aspx).  And as can be seen, we only have FromHBitMap (http://msdn.microsoft.com/en-us/library/system.drawing.image_members.aspx) available besides FromStream. We need to do a lot of things to draw an image from HBitMap so I decided to use FromStream.

The code is really simple.

FileStream strm = new FileStream(“CHMIcon.ico”, System.IO.FileMode.Open);
Bitmap bm = new Bitmap(strm);
imgList.Images.Add(bm);

This code compiled great. But when I tried to run it, I got exceptions such as not enough memory. I checked the icon file and it was around 3Kb. I googled for it and I didn’t get any straight answers. I finally decided to change the icon to jpg format instead and low and behold, everything started working perfectly.

Nobody in Microsoft told us that we cannot load icon files! Well, I tell it to you now, you cannot load ico files using this method. I wasted a couple of hours doing this before I decided to change format. An out of memory exception does not give much information and what the hell, I had my super computer resources available so there was no way it could go out of memory on an emulator. Microsoft needs to improve. Provide it in the MSDN for gods sake.

Anyways, no more ranting now since the stuff works finally. So you guys take care. The solution is more simpler then it looks.