Jump to content
Xtreme .Net Talk

Recommended Posts

  • Leaders
Posted
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?
[sIGPIC]e[/sIGPIC]
Posted

I couldn't 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...

 

here's an example.

I'll play some more with this, it should work for creating cursors too. I've not got 32bpp working yet, or multiple icons in 1 file, (or < 24bpp)

saveicon.zip

  • Leaders
Posted

Not bad. I found the specs for the file online, but I wasn't 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. I've 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.

[sIGPIC]e[/sIGPIC]

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...