Click here to Skip to main content
15,879,535 members
Articles / Desktop Programming / MFC
Article

The LoadImage() function doesn't load correctly a 4 bpp bitmap transparently

Rate me:
Please Sign up or sign in to vote.
3.80/5 (14 votes)
11 Feb 20033 min read 156.2K   635   24   30
LoadImage() used with the LR_LOADTRANSPARENT parameter doesn't behave as expected for 4bpp bitmaps.

Sample Image - LoadImageBug.jpg

Introduction

Last week I needed to load a bitmap transparently. It contained the icons I wanted to use in a tree control. That's when I detected the bug.

How should LoadImage() work? Well, of course you can find that in MSDN. But there's one thing I'd like to quote. When you use the function with the parameter LR_LOADTRANSPARENT, MSDN says:

Retrieves the color value of the first pixel in the image and replaces the corresponding entry in the color table with the default window color (COLOR_WINDOW). All pixels in the image that use that entry become the default window color. This value applies only to images that have corresponding color tables. Do not use this option if you are loading a bitmap with a color depth greater than 8bpp.

Well, my experience is that "retrieves the color of the first pixel in the image" should be replaced by "retrieves the first 8 bits in the image".

What do I mean?

I made a bitmap with a color depth of 4 bits per pixel. (see the base bitmap Bitmap01 in the figure).

Image 2

Now, I expected the red square in the middle of the icon to become transparent, if I made the lower left pixel in the bitmap red.

Image 3

That doesn't seem to work. (see the second tree item icon). If I make two pixels red, it works. (see the third tree item icon)

For a 8bpp bitmap it does work as expected as you can see in the figure.

I made a small application demonstrating the bug.

Conclusion

The function LoadImage() doesn't take at the color of the first pixel to determine the color that must be replaced by the default window color, it takes at the first 8 bits. (being two pixels in a 4bpp bitmap)

Workarounds

  1. Don't use 4bpp bitmaps
  2. If you like to use 4bpp bitmaps then make sure the two lower left pixels are of the same color. The color you want to be replaced by the window's color.
  3. Don't trust the transparently loading of the bitmap to LoadImage() and do it yourself: Use LoadImage() to load the bitmap as it is. Then replace the color of your choice with the window's color.

    If you're really interested in this solution take a look at the available code of the demo project.

Remarks

  • I am not able to test this out for VC7.
  • If someone would be so kind as to compile my little app in VC7 and let me know?
  • "the first pixel" of a bitmap is de the lower left pixel of the bitmap and not the upper left as you would expect. For the explanation see some remarks posted beneath this article.

Credits

Thanks to Davide Calabro for his article "CreateGrayScaleIcon" [^] where I got the inspiration for my function to change icon colors.

History

  • [12-Feb-2003]

    First version

  • [13-Feb-2003]

    Since some nice guys were so kind to yell that my article wasn't worth to be posted since it didn't give any workaround (which was not true), I added a real workaround. Thanks guys! :P

License

This article has no explicit license attached to it but may contain usage terms in the article text or the download files themselves. If in doubt please contact the author via the discussion board below.

A list of licenses authors might use can be found here


Written By
Software Developer (Senior)
Belgium Belgium
The first 5 years of my career I programmed in pure C. (Production automatisation software)
In Q4 of 1997 I switched to Visual C++/MFC. (Headend Management system for cable operators)
In Q1 of 2003 I changed job and started programming in JAVA (and Swing)
Later on I also worked in Adobe Flex, GWT and GXT.
Then, since our products became more web based, I started programming in JavaScript and Sencha ExtJS.
In my current job I program in Java, TypeScript and React.

Comments and Discussions

 
Generaladding HICON to items Pin
sarasara098765411-Oct-03 20:32
sarasara098765411-Oct-03 20:32 
GeneralReal workaround added Pin
Geert Delmeiren13-Feb-03 0:52
Geert Delmeiren13-Feb-03 0:52 
QuestionWant some more? Pin
Chopper12-Feb-03 5:52
Chopper12-Feb-03 5:52 
AnswerRe: Want some more? Pin
Marc Clifton12-Feb-03 6:41
mvaMarc Clifton12-Feb-03 6:41 
GeneralRe: Want some more? Pin
Chopper12-Feb-03 13:26
Chopper12-Feb-03 13:26 
AnswerRe: Want some more? Pin
l a u r e n12-Feb-03 8:39
l a u r e n12-Feb-03 8:39 
GeneralRe: Want some more? Pin
Chopper12-Feb-03 13:20
Chopper12-Feb-03 13:20 
AnswerRe: Want some more? Pin
Andreas Saurwein13-Feb-03 1:10
Andreas Saurwein13-Feb-03 1:10 
GeneralRe: Want some more? Pin
Chopper13-Feb-03 1:41
Chopper13-Feb-03 1:41 
GeneralRe: Want some more? Pin
Andreas Saurwein13-Feb-03 2:10
Andreas Saurwein13-Feb-03 2:10 
GeneralRe: Want some more? Pin
Chopper13-Feb-03 2:21
Chopper13-Feb-03 2:21 
GeneralRe: Want some more? Pin
Andreas Saurwein13-Feb-03 2:29
Andreas Saurwein13-Feb-03 2:29 
GeneralRe: Want some more? Pin
Chopper13-Feb-03 2:46
Chopper13-Feb-03 2:46 
GeneralRe: Want some more? Pin
Andreas Saurwein13-Feb-03 2:55
Andreas Saurwein13-Feb-03 2:55 
GeneralRe: Want some more? Pin
Chopper13-Feb-03 3:18
Chopper13-Feb-03 3:18 
GeneralRe: Want some more? Pin
Andreas Saurwein13-Feb-03 3:32
Andreas Saurwein13-Feb-03 3:32 
GeneralRe: Want some more? Pin
Chopper13-Feb-03 3:39
Chopper13-Feb-03 3:39 
GeneralRe: Want some more? Pin
Dean7018-Feb-03 23:27
Dean7018-Feb-03 23:27 
GeneralBottoms up, a different explanation Pin
Marc Clifton12-Feb-03 5:31
mvaMarc Clifton12-Feb-03 5:31 
GeneralThe first pixel explaination Pin
Anders Dalvander12-Feb-03 4:27
Anders Dalvander12-Feb-03 4:27 
GeneralRe: The first pixel explaination Pin
KarstenK12-Feb-03 4:40
mveKarstenK12-Feb-03 4:40 
GeneralRe: The first pixel explaination Pin
Marc Clifton12-Feb-03 5:26
mvaMarc Clifton12-Feb-03 5:26 
QuestionPoint of the article? Pin
Rama Krishna Vavilala12-Feb-03 2:00
Rama Krishna Vavilala12-Feb-03 2:00 
AnswerRe: Point of the article? Pin
Geert Delmeiren12-Feb-03 2:10
Geert Delmeiren12-Feb-03 2:10 
GeneralRe: Point of the article? Pin
Rama Krishna Vavilala12-Feb-03 4:48
Rama Krishna Vavilala12-Feb-03 4:48 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.