Click here to Skip to main content
Click here to Skip to main content
Go to top

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

, 11 Feb 2003
Rate this:
Please Sign up or sign in to vote.
LoadImage() used with the LR_LOADTRANSPARENT parameter doesn't behave as expected for 4bpp bitmaps.

Sample Image - LoadImageBug.jpg


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).

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.

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.


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)


  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.


  • 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.


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


  • [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


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


About the Author

Geert Delmeiren
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 since then I'm programming in JAVA. So, now I'm looking for a JAVA site as brilliant as I found this one for C++.
Two years ago I started also programming in Adobe Flex.

Comments and Discussions

Generaladding HICON to items Pinmembersarasara098765411-Oct-03 20:32 
GeneralReal workaround added PinmemberGeert Delmeiren13-Feb-03 0:52 
QuestionWant some more? PinmemberChopper12-Feb-03 5:52 
AnswerRe: Want some more? PinmemberMarc Clifton12-Feb-03 6:41 
GeneralRe: Want some more? PinmemberChopper12-Feb-03 13:26 
AnswerRe: Want some more? Pinmemberlauren12-Feb-03 8:39 
GeneralRe: Want some more? PinmemberChopper12-Feb-03 13:20 
AnswerRe: Want some more? PinmemberAndreas Saurwein13-Feb-03 1:10 
GeneralRe: Want some more? PinmemberChopper13-Feb-03 1:41 
GeneralRe: Want some more? PinmemberAndreas Saurwein13-Feb-03 2:10 

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

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

| Advertise | Privacy | Mobile
Web04 | 2.8.140916.1 | Last Updated 12 Feb 2003
Article Copyright 2003 by Geert Delmeiren
Everything else Copyright © CodeProject, 1999-2014
Terms of Service
Layout: fixed | fluid