Icon.Save - Results are ugly

snarfblam

Mega-Ultra Chicken
Joined
Jun 10, 2003
Messages
1,832
Location
USA
User Rank
*Expert*
The Icon.Save function saves icons in a 4-bit format. Needless to say, this is pretty ugly. Does anyone know a way to save a GDI+ Icon object as a 24/32 bit icon file or a way to save a GDI Icon from a handle to a 24/32 bit icon file?
 
I couldnt find a built in way either. The only way I could find was to create the whole icon from scratch. The structures required are documented in some dusty corner of the msdn library, with some examples of using them here and there on the net.
The icon uses two bitmaps, one is 1bpp and gets ANDed with the background. The other is color and gets XORed with the result. (may have got the order wrong, whatever).
So we can use lockbits and then marshal.copy to the xor bitmap. And then scan through the array of colors and compare with the transparency color to set the AND bitmap.
The devil is in the detail - each row of pixels has to end on a 4 byte boundary for the xor bitmap. For the AND bitmap, it is 1bpp, and the total number of bytes used for a row also needs padding to 4 bytes. Also the structures are not very well documented, with conflicting information.
Once done, you just churn through the structures with a binarywriter...

heres an example.
Ill play some more with this, it should work for creating cursors too. Ive not got 32bpp working yet, or multiple icons in 1 file, (or < 24bpp)
 

Attachments

Not bad. I found the specs for the file online, but I wasnt in the mood for jumping in and making my own complete Win32 icon class. Guess you have more patience than I.

What I was trying to do is create a program that will batch process a folder and convert PNGs to ICOs and vice-versa, so that all my graphics that I frequently use for my applications will be available in both formats, saving me the trouble of firing up GIMP or Photoshop and converting them each time I want to use a PNG as an icon. Ive made the program one-way so far; using the Icon.ToBitmap method to easily create bitmaps from icons. The problem is that I was using Bitmap.GetHicon to convert bitmaps to icons, which worked great, but Icon.Save creates nasty 16 color icon files.

Thanks.
 
Back
Top