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
Rate this:
Please Sign up or sign in to vote.
Replacing a color by another in transparent bitmaps.

Replace color screenshot

Introduction

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

Limitations

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 hBmp2 = LoadBitmap(g_hinstance,MAKEINTRESOURCE(IDB_SAMPLEBITMAP));
HBITMAP hBmp = ReplaceColor(hBmp2,0xff0000,0x00ff00); // replace blue by green
DeleteObject(hBmp2);

// Use your modified Bitmap here 

DeleteObject(hBmp);

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 drochette@coldcat.fr
// 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
//
//-------------------------------------------------------------------------------
HBITMAP ReplaceColor(HBITMAP hBmp,COLORREF cOldColor,COLORREF cNewColor,HDC hBmpDC)
{
    HBITMAP RetBmp=NULL;
    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; 
                ZeroMemory(&RGB32BitsBITMAPINFO,sizeof(BITMAPINFO));
                RGB32BitsBITMAPINFO.bmiHeader.biSize=sizeof(BITMAPINFOHEADER);
                RGB32BitsBITMAPINFO.bmiHeader.biWidth=bm.bmWidth;
                RGB32BitsBITMAPINFO.bmiHeader.biHeight=bm.bmHeight;
                RGB32BitsBITMAPINFO.bmiHeader.biPlanes=1;
                RGB32BitsBITMAPINFO.bmiHeader.biBitCount=32;

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

                HBITMAP DirectBitmap = CreateDIBSection(DirectDC, 
                                       (BITMAPINFO *)&RGB32BitsBITMAPINFO, 
                                       DIB_RGB_COLORS,
                                       (void **)&ptPixels, 
                                       NULL, 0);
                if (DirectBitmap)
                {
                    // here DirectBitmap!=NULL so ptPixels!=NULL no need to test
                    HGDIOBJ PreviousObject=SelectObject(DirectDC, DirectBitmap);
                    BitBlt(DirectDC,0,0,
                                   bm.bmWidth,bm.bmHeight,
                                   BufferDC,0,0,SRCCOPY);

                       // here the DirectDC contains the bitmap

                    // Convert COLORREF to RGB (Invert RED and BLUE)
                    cOldColor=COLORREF2RGB(cOldColor);
                    cNewColor=COLORREF2RGB(cNewColor);

                    // 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)
                       SelectObject(DirectDC,PreviousObject);

                    // finish
                    RetBmp=DirectBitmap;
                }
                // clean up
                DeleteDC(DirectDC);
            }            
            if (hTmpBitmap)
            {
                SelectObject(hBmpDC, hBmp);
                DeleteObject(hTmpBitmap);
            }
            SelectObject(BufferDC,PreviousBufferObject);
            // BufferDC is now useless
            DeleteDC(BufferDC);
        }
    }
    return RetBmp;
}

License

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

 
GeneralMy vote of 5 Pinmember24532452433-Oct-12 14:35 
GeneralMy vote of 3 PinmemberMukit, Ataul5-Feb-12 19:31 
GeneralRe: My vote of 3 Pinmembersgllama5-Feb-12 22:14 
GeneralRe: My vote of 3 Pinmembersgllama5-Feb-12 22:26 
GeneralRe: My vote of 3 PinmemberMukit, Ataul5-Feb-12 22:29 
GeneralMy vote of 5 PinmemberAbinash Bishoyi5-Feb-12 6:11 
Questiongreate code Pinmemberszk.sh7-Dec-11 16:17 
QuestionRewrite available for .Net either VB.Net or C#? PinmemberTim8w2-Jul-08 5:50 
GeneralBrilliant PinmemberAli Imran Khan Shirani29-Jun-08 5:25 
Generalcan't build Pinmemberk_meleon20-Mar-08 22:44 
GeneralArtist needs help Pinmemberpxfragonard29-Jan-07 13:32 
GeneralProblem on WinCE 3.0 PinmemberAllen228-Sep-06 5:29 
GeneralRe: Problem on WinCE 3.0 PinmemberDimitri Rochette28-Sep-06 8:58 
GeneralA little shorter variant of "BlitReplaceColor" PinmemberYuri Machlin23-Jun-06 1:07 
GeneralRe: A little shorter variant of "BlitReplaceColor" PinmemberDimitri Rochette24-Jun-06 9:30 
Generalconverting values PinmemberdSolariuM27-Apr-06 11:29 
GeneralRe: converting values PinmemberDimitri Rochette27-Apr-06 23:22 
GeneralRe: converting values PinmemberdSolariuM28-Apr-06 3:15 
Hi,
Thanks for your reply...It's right but it doesn't work for me..
I use these(foolish,I know,It's only for test,Just to be sure about sending)codes to fill the pt array from 3 chars. and it gives
me something like inverted colors. But I am sure that these colors are not inverted.
Please help me.I need your help...
 
////////////////////////////////////////////////////////////////////////////////////////////////////////
.
.
.
.
bool t=false;
long lc=NULL;
UINT ic=NULL;
UINT *pt=new UINT[1024*768];
 
void CMfcDlg::OnBnClickedButton2()
{
char cRed,cGreen,cBlue;
for (int i=0;i<=((1024*768));i++)
{
char cRed= (ptPixels[i] & 0xff0000)>>16;
char cGreen= (ptPixels[i] & 0x00ff00)>>8 ;
char cBlue= (ptPixels[i] & 0x0000ff);
myFunc(cRed,cGreen,cBlue);
}
t=true;
myFunc(NULL,NULL,NULL);
}
 
void CMfcDlg::myFunc(char chR,char chG,char chB)
{
if (!t)
{
lc++;
pt[lc]=(chR <<16)|(chG <<8)|(chB);
}
if (t)
{
CWindowDC dcc(GetDesktopWindow());
BITMAPINFO RGB32BitsBITMAPINFO;
ZeroMemory(&RGB32BitsBITMAPINFO,sizeof(BITMAPINFO));
RGB32BitsBITMAPINFO.bmiHeader.biSize=sizeof(BITMAPINFOHEADER);
RGB32BitsBITMAPINFO.bmiHeader.biWidth=1024;
RGB32BitsBITMAPINFO.bmiHeader.biHeight=768;
RGB32BitsBITMAPINFO.bmiHeader.biPlanes=1;
RGB32BitsBITMAPINFO.bmiHeader.biBitCount=32;
StretchDIBits(dcc.GetSafeHdc(),0,0,200,500,0,0,200,500,pt,
(BITMAPINFO*)&RGB32BitsBITMAPINFO,DIB_RGB_COLORS,SRCCOPY);
lc=0;
ic=0;
t=false;
}
}
//////////////////////////////////////////////////////////////////////////////////////
 
Every new thing you learn,Gives you a new personality.
GeneralBitmap bit access PinmemberGeorgi Petrov28-Dec-04 2:56 
GeneralRe: Bitmap bit access PinmemberDimitri Rochette28-Dec-04 9:43 
GeneralRe: Bitmap bit access PinmemberGeorgi Petrov28-Dec-04 20:16 
QuestionWhy not sent changed bmmp to clipboard? Pinmembersunwolf_00118-Oct-04 16:16 
GeneralLoading bitmap Pinmembervivadotnet23-Jun-04 8:23 
GeneralRe: Loading bitmap PinmemberDimitri Rochette23-Jun-04 10:30 
GeneralThanks for the code snipit PinmemberSAHorowitz19-Apr-04 4:03 

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.140709.1 | Last Updated 5 Feb 2012
Article Copyright 2002 by Dimitri Rochette
Everything else Copyright © CodeProject, 1999-2014
Terms of Service
Layout: fixed | fluid