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

AlphaBlend function and 32 bit bitmaps with alpha channel

By , 18 Dec 2008
 

Introduction

This article shows how the GDI function AlphaBlend is used to display and overlay 32 bit bitmaps with an alpha channel. It is mainly a hint, which would have saved me hours of research.

Background

The code and the idea of this article comes from a setup bootstrapper project. I want to be able to compose background images for wizard pages from 32 bit bitmaps with alpha transparency. That should save space as described in MSE-iT Setup Bootstrapper - Background Bitmap Resources.

Code Snippet

// the blending of the bitmap is quite simple
// (in the OnPaint() method or the WM_PAINT message handler: 
BITMAP bm;
GetObject(background, sizeof(BITMAP), &bm);
AlphaBlend(hdcMemWork, 0,0, bm.bmWidth, bm.bmHeight, hdcMem, 0, 0, 
           bm.bmWidth, bm.bmHeight, blendFkt)
SelectObject(hdcMem, hbmOld);

// The tricky thing is to pre multiply the alpha channel
// values after loading the 32 bit bitmaps
if(bmInfo.bmiHeader.biBitCount == 32 )
// Alpha Channel
{
    // pre-multiply rgb channels with alpha channel
    for (int y=0; y<bmInfo.bmiHeader.biHeight; ++y)
    {
        BYTE *pPixel= (BYTE *) lpDIBBits + bmInfo.bmiHeader.biWidth * 4 * y;
        for (int x=0; x<bmInfo.bmiHeader.biWidth ; ++x)
        {
            pPixel[0]= pPixel[0]*pPixel[3]/255;
            pPixel[1]= pPixel[1]*pPixel[3]/255;
            pPixel[2]= pPixel[2]*pPixel[3]/255;
            pPixel+= 4;
        }
    }
}

Points of interest

I played around a long time until AlphaBlend began working. At least the trick was to pre multiply the color values of the bitmap with the alpha value. Then, everything worked fine. Be careful with the width and height you provide to AlphaBlend(). If this exceeds the bounds of the bitmap, nothing is displayed. I prefer getting the width and height directly out of the bitmap data as shown.

Alpha blending effect

This is the empty background image of the bootstrapper wizard:

Setup-Bootstrapper-Background.jpg

This is the left side image to blend over the background (transparency is black):

Setup-Bootstrapper-Product-Case.jpg

After adding additional images for localization, this is the resulting page:

Setup-Bootstrapper-Welcome-Page.jpg

Source code and working sample

You can check out the source code and the working sample in the original bootstrapper project: MSE-iT Setup Bootstrapper Project on CodePlex.

License

This article, along with any associated source code and files, is licensed under The GNU General Public License (GPLv3)

About the Author

Thomas Maierhofer
CEO MSE-iT
Germany Germany
Member
I'm the CEO of MSE-iT Reisebürosoftware, a software development company in Germany making travel agency accounting software.
 
We release some library stuff under GPL or LGPL, so everybody can use it. These are our Open-Source projects so far:
 
Spell Checking, Hyphenation and Thesaurus for .NET

.NET spell checker, hyphenation and thesaurus based on the Open Offlice spell checker Hunspell. Here is a life Demo: Spell check, hyphenation and thesaurus reference project for NHunspell on ASP.NET.
 
jQuery Plugins

jQuery Background Canvas Plugin Inserts a HTML5 CANVAS element behind any HTML element and allows to draw on it with JavaScript.

Sign Up to vote   Poor Excellent
Add a reason or comment to your vote: x
Votes of 3 or less require a comment

Comments and Discussions

 
Hint: For improved responsiveness ensure Javascript is enabled and choose 'Normal' from the Layout dropdown and hit 'Update'.
You must Sign In to use this message board.
Search this forum  
    Spacing  Noise  Layout  Per page   
GeneralMy vote of 1memberMember 294716418 Apr '10 - 23:01 
GeneralMy vote of 1memberjunhee5 Aug '09 - 18:36 
GeneralMy vote of 2memberLeo Davidson18 Dec '08 - 21:26 
GeneralRe: My vote of 2memberThomas Maierhofer19 Dec '08 - 1:58 
GeneralMy vote of 2memberAlexandre GRANVAUD18 Dec '08 - 21:02 
GeneralRe: My vote of 2memberThomas Maierhofer19 Dec '08 - 1:59 

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

Permalink | Advertise | Privacy | Mobile
Web04 | 2.6.130523.1 | Last Updated 18 Dec 2008
Article Copyright 2008 by Thomas Maierhofer
Everything else Copyright © CodeProject, 1999-2013
Terms of Use
Layout: fixed | fluid