Click here to Skip to main content
11,805,266 members (50,955 online)
Click here to Skip to main content


, 12 Dec 2005 45.2K 963 48
Rate this:
Please Sign up or sign in to vote.
This articles illustrates alpha-blending two bitmaps.



This code snippet shows how to alpha-blend two bitmaps by using the linear interpolation formula.


Using the code

Here is a function which can be used in OnPaint() or OnDraw(). Actually, in the sample project, I didn't prepare a function, I put the code directly in the OnPaint() method. In this function, the new blended bitmap is placed on a second bitmap. You can implement the code and create a third bitmap for the new bitmap if you need.

The second parameter, double blend, must be in [0,1). This number is for the first bitmap, and the second number is 1-blend for the second bitmap.

void CAlphaBlendingDlg::AlphaBlend(CDC *pDC, double blend) 
    BITMAP bmpX,bmpY;
    CBitmap bmp1,bmp2;

    UINT* bmpBuffer=(UINT*) GlobalAlloc(GPTR, 
                            bmpX.bmWidthBytes * bmpX.bmHeight);
    bmp1.GetBitmapBits(bmpX.bmWidthBytes * bmpX.bmHeight, bmpBuffer);

    UINT* bmpBuffer2=(UINT*) GlobalAlloc(GPTR, 
                             bmpY.bmWidthBytes * bmpY.bmHeight);
    bmp2.GetBitmapBits(bmpY.bmWidthBytes * bmpY.bmHeight, bmpBuffer2);

    int nSize = bmpY.bmWidth * bmpY.bmHeight;

    for (int i = 0; i < nSize; i++)
        int abR = (int) (GetR(bmpBuffer[i]) * blend + 
                        (1-blend) * GetR(bmpBuffer2[i]));
        int abG = (int) (GetG(bmpBuffer[i]) * blend + 
                        (1-blend) * GetG(bmpBuffer2[i]));
        int abB = (int) (GetB(bmpBuffer[i]) * blend + 
                        (1-blend) * GetB(bmpBuffer2[i]));

        bmpBuffer2[i] = RGB(abB, abG, abR);

    bmp2.SetBitmapBits(bmpX.bmWidthBytes * bmpX.bmHeight, bmpBuffer2);

    CDC memdc;
                bmpX.bmHeight, &memdc, 0, 0, SRCCOPY);



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

Ozge Colak
Software Developer Ibtech
Turkey Turkey

You may also be interested in...

Comments and Discussions

QuestionHow to alpha-blend three (or more) bitmaps? Pin
Tee Moore4-Dec-07 22:30
memberTee Moore4-Dec-07 22:30 
AnswerRe: How to alpha-blend three (or more) bitmaps? Pin
engelbert16-Jan-08 20:00
memberengelbert16-Jan-08 20:00 
GeneralVery cool article! Pin
Savior99923-Nov-06 1:48
memberSavior99923-Nov-06 1:48 
GeneralYou forgot to cap each color Pin
CBasicNet12-Dec-05 21:52
memberCBasicNet12-Dec-05 21:52 
GeneralRe: You forgot to cap each color - No, they are capped Pin
Ozge Colak13-Dec-05 0:27
memberOzge Colak13-Dec-05 0:27 

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
Web03 | 2.8.151002.1 | Last Updated 12 Dec 2005
Article Copyright 2005 by Ozge Colak
Everything else Copyright © CodeProject, 1999-2015
Layout: fixed | fluid