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

AlphaBlend function and 32 bit bitmaps with alpha channel

, 18 Dec 2008
Rate this:
Please Sign up or sign in to vote.
Shows the usage of AlphaBlend on 32 bit bitmaps to save space in a bootstrapper.

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

I'm working on a new project called Crawler-Lib. It is a generalized back-end processing and hosting framework. Please take a look at it: Crawler-Lib Homepage
 
I have also a YouTube channel for this: Crawler-Lib YouTube Channel

Comments and Discussions

 
GeneralMy vote of 1 PinmemberMember 294716418-Apr-10 23:01 
GeneralMy vote of 1 Pinmemberjunhee5-Aug-09 18:36 
GeneralMy vote of 2 PinmemberLeo Davidson18-Dec-08 21:26 
GeneralRe: My vote of 2 PinmemberThomas Maierhofer19-Dec-08 1:58 
GeneralMy vote of 2 PinmemberAlexandre GRANVAUD18-Dec-08 21:02 
GeneralRe: My vote of 2 PinmemberThomas Maierhofer19-Dec-08 1:59 

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
Web01 | 2.8.140721.1 | Last Updated 18 Dec 2008
Article Copyright 2008 by Thomas Maierhofer (Tom)
Everything else Copyright © CodeProject, 1999-2014
Terms of Service
Layout: fixed | fluid