Changing Colors in Bitmaps and Making Icons from Them

Environment: MFC/GDI

The day has come when I need to have a large number of identical icons with only one difference—some colors will change from icon to icon. I found the solution for making all of my icons from a single bitmap, and changing colors on the fly. So, I wrote my CCloneBitmap class. It is derived from CBitmap and is able to change colors and export 32×32 icons.

It is very simple to use. Just follow these steps:

  1. Load the bitmap using the LoadBitmap() function.
  2. Make a clone of this bitmap by calling a Clone() method.
  3. Change colors of the bitmap by calling a ChangeColor() method.
  4. You can make a 32×32 icon from this bitmap (if bitmap is 32×32).

Here is a simple example of how to use this class:


HBITMAP hBmp;
CCloneBitmap bmpClone;
HICON hIcon;
hBmp=LoadBitmap(AfxGetResourceHandle(),
MAKEINTRESOURCE(ID_LIGHTCAR));
if(hBmp!=NULL)
{
bmpClone.Clone(hBmp);
DeleteObject(hBmp);
bmpClone.ChangeColor(IRGB(0,0,0), IRGB(255,0,0));
// change BLACK pixels to RED ones.
hIcon=bmpClone.MakeIcon(IRGB(255,255,255));
// make icon, using WHITE color as transparent.
}

Note: Remember to use the IRGB() macro instead of RGB().

If you have a color in the COLORREF variable, use the INVERSECOLOR() macro:

bmpClone.ChangeColor(IRGB(0,0,0), INVERSECOLOR(myColor));

Downloads


Download source – 2 Kb

More by Author

Must Read