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 32x32 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 32x32 icon from this bitmap (if bitmap is 32x32).

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


Comments

  • Help with dynamic bitmap

    Posted by Legacy on 02/10/2004 12:00am

    Originally posted by: VIjay Shan

    Hi,
    I found your article to be extremely helpful, though had a couplea questions how do i use this class if i have a modified bitmap (cbitmap*). when i paste this bitmap into the clipboard it works fine but when i get a handle and pass it to the clone function and use makeicon it just gives me a black square back. whereas when i try the samething with a preproduced bitmap it works fine. Any kinda help would be appreciated.

    Vijay

    Reply
  • Great, but if you (or someone) would please....

    Posted by Legacy on 05/30/2003 12:00am

    Originally posted by: Chris

    Hello, I'm a Borland C++ Builder programmer and I'd love to use that code, I have MSVC++ and have dabbled in it... I've managed to convert most of the code, but I'm having problems, I was wondering if you could convert it into standard Windows SDK code(nothing like CDC or CBitmap used) and then post it for all programmers to use.
    Thanks and have a nice day. :)

    Reply
Leave a Comment
  • Your email address will not be published. All fields are required.

Top White Papers and Webcasts

  • Learn How A Global Entertainment Company Saw a 448% ROI Every business today uses software to manage systems, deliver products, and empower employees to do their jobs. But software inevitably breaks, and when it does, businesses lose money -- in the form of dissatisfied customers, missed SLAs or lost productivity. PagerDuty, an operations performance platform, solves this problem by helping operations engineers and developers more effectively manage and resolve incidents across a company's global operations. …

  • Live Event Date: December 18, 2014 @ 2:00 p.m. ET / 11:00 a.m. PT The Internet of Things (IoT) incorporates physical devices into business processes using predictive analytics. While it relies heavily on existing Internet technologies, it differs by including physical devices, specialized protocols, physical analytics, and a unique partner network. To capture the real business value of IoT, the industry must move beyond customized projects to general patterns and platforms. Check out this upcoming webcast …

Most Popular Programming Stories

More for Developers

RSS Feeds