Click here to Skip to main content
Click here to Skip to main content

How to replace a color in an HBITMAP

, 5 Feb 2012 Public Domain
Rate this:
Please Sign up or sign in to vote.
Replacing a color by another in transparent bitmaps.

Replace color screenshot


When I wrote this function my problem was to replace one color by another on transparent bitmaps. My images were resources bitmaps, which I store in an ImageList for easy transparency.

There is no easy way to directly access a bitmap's pixel on Win32. If you're interested in doing that, this article may help you understand the usage of CreateDIBSection.

  • If you have to load a bitmap from a resource and make many color replacements, or if you have to change a color in a HBITMAP, this function is for you.
  • If you have a bitmap in a resource and want to replace one or more colors on load, it's better to use CreateMappedBitmap. You can find in the sample program a ReplaceColor function which uses CreateMappedBitmap.

I've made the same code using only BitBlt. BitBlt is really fast but the creation of the mask bitmap is so slow that the whole function is twice slower than the code using CreateDIBSection (sources are in the sample).


My code always return a 32 bit bitmap. If you need to keep your original bitmap bit per pixels, you'll have two options:

  • You can rewrite it with GetDIBits. You will need special code for monochromes to 32 bit bitmaps.
  • You can BitBlt the 32 bit bitmap to your less color DC. It's easy, slow, and you will lose some colors in the process.

Sample program

The sample program you can download does the following:

  • Creates a window in WinMain.
  • Loads a bitmap from resources WM_NCCREATE.
  • Adds this bitmap to an ImageList WM_NCCREATE.
  • Uses ReplaceColor to get a copy of the bitmap with one color replaced by another (4 times) WM_NCCREATE.
  • Displays all the stored images in the window WM_PAINT.
  • Cleans everything WM_DESTROY.

Easy to use

A short sample of the usage of ReplaceColor:

HBITMAP hBmp = ReplaceColor(hBmp2,0xff0000,0x00ff00); // replace blue by green

// Use your modified Bitmap here 


ReplaceColor source code

The ReplaceColor function is a pure Win32 function. It doesn't make any use of MFC. Its code is standalone, you can cut and paste it without modifications to your code.

#define COLORREF2RGB(Color) (Color & 0xff00) | ((Color >> 16) & 0xff) \
                                 | ((Color << 16) & 0xff0000)

// ReplaceColor
// Author    : Dimitri Rochette
// Specials Thanks to Joe Woodbury for his comments and code corrections
// Includes  : Only <windows.h>

// hBmp         : Source Bitmap
// cOldColor : Color to replace in hBmp
// cNewColor : Color used for replacement
// hBmpDC    : DC of hBmp ( default NULL ) could be NULL if hBmp is not selected
// Retcode   : HBITMAP of the modified bitmap or NULL for errors
    if (hBmp)
        HDC BufferDC=CreateCompatibleDC(NULL);    // DC for Source Bitmap
        if (BufferDC)
            HBITMAP hTmpBitmap = (HBITMAP) NULL;
            if (hBmpDC)
                if (hBmp == (HBITMAP)GetCurrentObject(hBmpDC, OBJ_BITMAP))
                hTmpBitmap = CreateBitmap(1, 1, 1, 1, NULL);
                SelectObject(hBmpDC, hTmpBitmap);

            HGDIOBJ PreviousBufferObject=SelectObject(BufferDC,hBmp);
            // here BufferDC contains the bitmap
            HDC DirectDC=CreateCompatibleDC(NULL); // DC for working
            if (DirectDC)
                // Get bitmap size
                BITMAP bm;
                GetObject(hBmp, sizeof(bm), &bm);

                // create a BITMAPINFO with minimal initilisation 
                // for the CreateDIBSection
                BITMAPINFO RGB32BitsBITMAPINFO; 

                // pointer used for direct Bitmap pixels access
                UINT * ptPixels;    

                HBITMAP DirectBitmap = CreateDIBSection(DirectDC, 
                                       (BITMAPINFO *)&RGB32BitsBITMAPINFO, 
                                       (void **)&ptPixels, 
                                       NULL, 0);
                if (DirectBitmap)
                    // here DirectBitmap!=NULL so ptPixels!=NULL no need to test
                    HGDIOBJ PreviousObject=SelectObject(DirectDC, DirectBitmap);

                       // here the DirectDC contains the bitmap

                    // Convert COLORREF to RGB (Invert RED and BLUE)

                    // After all the inits we can do the job : Replace Color
                    for (int i=((bm.bmWidth*bm.bmHeight)-1);i>=0;i--)
                        if (ptPixels[i]==cOldColor) ptPixels[i]=cNewColor;
                    // little clean up
                    // Don't delete the result of SelectObject because it's 
                    // our modified bitmap (DirectBitmap)

                    // finish
                // clean up
            if (hTmpBitmap)
                SelectObject(hBmpDC, hBmp);
            // BufferDC is now useless
    return RetBmp;


This article, along with any associated source code and files, is licensed under A Public Domain dedication


About the Author

Dimitri Rochette
Software Developer (Senior)
France France
No Biography provided

Comments and Discussions

GeneralRe: Possible problems with the code PinmemberHarald Krause27-Sep-02 2:13 
GeneralRe: Possible problems with the code PinsussAnonymous17-Feb-03 17:34 
General. PinmemberGoran Mitrovic4-Sep-02 14:19 
GeneralRe: . PinmemberDimitri Rochette6-Sep-02 11:52 
GeneralRe: . PinmemberGoran Mitrovic6-Sep-02 15:27 
Generala stupid question... PinmemberPaolo Messina4-Sep-02 12:55 
GeneralRe: a stupid question... PinmemberChristian Graus4-Sep-02 13:03 
GeneralRe: a stupid question... PinmemberPaolo Messina4-Sep-02 14:25 
Christian Graus wrote:
Creating the DIB means he can access the bits directly, otherwise he'd have to use Get/SetPixel which is really SLOW. Also, he's using an int to represent a colour, which is 32 bits, so he needs to transform the image to be 32 bit for this code to work.
Yeah, I know it was stupid. Blush | :O
"airplane is cool, but space shuttle is even better" (J. Kaczorowski)

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 | Terms of Use | Mobile
Web01 | 2.8.150327.1 | Last Updated 5 Feb 2012
Article Copyright 2002 by Dimitri Rochette
Everything else Copyright © CodeProject, 1999-2015
Layout: fixed | fluid