Click here to Skip to main content
13,044,428 members (118,748 online)
Click here to Skip to main content
Add your own
alternative version

Tagged as


20 bookmarked
Posted 4 Apr 2011

A very basic alpha blend algorithm

, 19 Apr 2011
Rate this:
Please Sign up or sign in to vote.
A simple implementation of alpha blending technique displaying the basic mechanism behind it
Looking over the internet, I didn't find any manual implementation of the AlphaBlend function supported by Windows. Due to some bindings of my project, I couldn't use the AlphaBlend provided by windows, so I had to implement it on my own.

I hope it (atleast the concept) becomes useful for some:

(Note: This will only work if the source and destination dc bitmaps are equal in size.)

void AlphaBlend(CDC* pDC, int xDest, int yDest, int nDestWidth, int nDestHeight,
   CDC* pSrcDC, int xSrc, int ySrc, BYTE uAlphaValue)
    BOOL bProceed = TRUE;
    CDC memDC;
        bProceed = FALSE;
    CBitmap memBmp;
        if(!memBmp.CreateCompatibleBitmap(pDC, nDestWidth, nDestHeight))
            bProceed = FALSE;
        CBitmap* pOldBmp = memDC.SelectObject(&memBmp);
        memDC.BitBlt(0, 0, nDestWidth, nDestHeight, pDC, 0, 0, SRCCOPY);
        CDC* pDestDC = &memDC;
        BYTE r1, r2, rDest;
        BYTE g1, g2, gDest;
        BYTE b1, b2, bDest;
        BYTE av = uAlphaValue; // Alpha value BYTE
        BYTE rem = 255 - av; // Remaining fraction
        COLORREF clrPixelDest, clrPixelSrc;
        for(int dy = yDest, sy = ySrc; dy < nDestHeight; dy++, sy++)
            for(int dx = xDest, sx = xSrc; dx < nDestWidth; dx++, sx++)
                clrPixelDest = pDestDC->GetPixel(dx, dy);
                r1 = GetRValue(clrPixelDest);
                g1 = GetGValue(clrPixelDest);
                b1 = GetBValue(clrPixelDest);
                clrPixelSrc = pSrcDC->GetPixel(sx, sy);
                r2 = GetRValue(clrPixelSrc);
                g2 = GetGValue(clrPixelSrc);
                b2 = GetBValue(clrPixelSrc);
                rDest = (r1*rem + r2*av) / 255;
                gDest = (g1*rem + g2*av) / 255;
                bDest = (b1*rem + b2*av) / 255;
                pDestDC->SetPixel(dx, dy, RGB(rDest, gDest, bDest));
        pDC->BitBlt(0, 0, nDestWidth, nDestHeight, &memDC, 0, 0, SRCCOPY);


This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


About the Author

Mukit, Ataul
Chief Technology Officer Rational Technologies
Bangladesh Bangladesh
If you are not in - you are out !
- Chapter 1

You may also be interested in...

Comments and Discussions

GeneralThank you. Pin
Conrad de Wet26-Aug-13 8:02
memberConrad de Wet26-Aug-13 8:02 
GeneralReason for my vote of 5 OK! Pin
Manfred R. Bihy11-Apr-11 3:40
mvpManfred R. Bihy11-Apr-11 3:40 
GeneralMaybe sledgehammer, wud u be kind enough to post an improvem... Pin
Mukit, Ataul6-Apr-11 0:26
memberMukit, Ataul6-Apr-11 0:26 
GeneralReason for my vote of 1 *NEVER* use GetPixel / SetPixel in a... Pin
SledgeHammer015-Apr-11 4:40
memberSledgeHammer015-Apr-11 4:40 
GeneralRe: Acknowledged.. Although since GetPixel and SetPixel is done ... Pin
Mukit, Ataul5-Apr-11 18:15
memberMukit, Ataul5-Apr-11 18:15 
GeneralExcellent! Pin
projectzombie25-Oct-11 17:34
memberprojectzombie25-Oct-11 17:34 
AnswerRe: Excellent! Pin
Mukit, Ataul25-Oct-11 18:14
memberMukit, Ataul25-Oct-11 18:14 
GeneralPathetic Pin
Mukit, Ataul15-May-11 0:28
memberMukit, Ataul15-May-11 0:28 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.

Permalink | Advertise | Privacy | Terms of Use | Mobile
Web01 | 2.8.170713.1 | Last Updated 19 Apr 2011
Article Copyright 2011 by Mukit, Ataul
Everything else Copyright © CodeProject, 1999-2017
Layout: fixed | fluid