Click here to Skip to main content
15,507,266 members
Please Sign up or sign in to vote.
4.00/5 (1 vote)
See more:
The structure ICONINFO has two fields hbmMask & hbmColor. These fields are of type HIBITMAP.

I want to convert bitmap to icon. When I load bitmap file, bitmap handle HIBITMAP is returned. Using this, how can I get values for hbmMask & hbmColor which represent bitmask bitmap & color bitmap. If I set both filieds to HIBITMAP of the loaded bitmap, I get icon but without colors.

Can we extract the proper values for these fields from HIBITMAP?

1 solution

In the way you asked, no. Simply because an Icon is actually two bitmaps: an image (defines the color of the pixels) and a mask (defines which pixels have to be painted and which not, and hence be transparent).

An HBITMAP is just one image, so you cannot "extract" the other.
But you can "guess" it algorithmically. for example:

A) make the mask completly "black": all pixels will be drawn (no transparency)

B) look the pixel of the image an guess a color you may use as a "transparent color". Create a mask that is "white" where the image has that color. (if the image is a drawing, you can use the topleft pixel as "background color")

C) Same as B, but consider transparent also colors that are "close" under a given approximation to the chosen color.

A is trivial, B is simple (just require a BitBlt of a color image into a monochrome bitmap) C requires more parsing of the picture.
Share this answer
Niklas L 3-Mar-11 17:09pm    
Voted 5.
In addition to C. One can look at the hue of the color to determine if the color at a given pixel is 'close enough' to a reference value.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900