Click here to Skip to main content
15,888,610 members
Articles / Desktop Programming / MFC

Per Pixel Alpha Blend

Rate me:
Please Sign up or sign in to vote.
4.73/5 (25 votes)
20 Apr 20012 min read 474.3K   15.2K   120   99
Demonstrates how to create per pixel alpha blending windows

Introduction

In this article, I will give you some source code to create per pixel alpha blending windows.

First, some images:

Nice, isn't it?

For creating a per-pixel window, we are going to use:

  • One 24bit Bitmap with alpha channel
  • The GDI function, UpdateLayeredWindow

24bit Bitmap with Alpha Channel

The most tricky part of creating the bitmap is finding a simple file format that has an embedded alpha channel.

The selected file format for the first incarnation of this article was PSP (Paint Shop Pro™ from Jasc®), but now the recommended file format has been changed to PNG (Portable Network Graphics).

For loading PNG files, I use the paintlib library. This library can also load files from PCX, TGA and TIFF formats.

This demo application does not use paintlib directly; instead it uses a paintlib wrapper DLL, ImgDecoder.Dll.

For loading PSP files, I've created a function called LoadPSPFileToDIB (PSPLoader.h/cpp).

UpdateLayeredWindow

To use this function, we must have a top-level window (in this sample, this is implemented by the class CDummyDialog) with the extended style WS_EX_LAYERED set.

Before we can pass the bitmap to the UpdateLayeredWindow function, we must pre-multiply the rgb channels with the alpha channel. For this, you just have to call the PreMultiplyRGBChannels function (take a look at CPerpixelalphaDlg::DoLoadFile).

From here on, read the source code in CPerpixelalphaDlg::DoUpdateDummyDialog.

Installing & Building

  1. Make sure you have the SDK for Windows® NT 5.0.
  2. Download and extract the source code of this article.
  3. Build and enjoy!

If you want to build the ImgDecoder.Dll, you also have to:

  1. Download paintlib and all the required libraries
  2. Download the ImgDecoder project
  3. Extract the ImgDecoder project to the paintlib library
  4. Build the ImgDecoder project
  5. Copy the resulting ImgDecoder.dll and ImgDecoder.lib to the bin directory of this article's demo application.

License

  1. You must mention my name in your application documentation/help box.
  2. I would appreciate it if you message me on the article forum letting me know that you are using this code.
  3. I cannot be held responsible for any kind of data loss caused directly or indirectly by the use of this code - use it at your own risk.
  4. You cannot sell this code - this is free software.

Don't forget if you have any constructive thoughts, please leave a comment below!

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.


Written By
Web Developer
Portugal Portugal
This member has not yet provided a Biography. Assume it's interesting and varied, and probably something to do with programming.

Comments and Discussions

 
GeneralUnicode compilation Pin
Amit Sindhwani31-Oct-02 3:46
sussAmit Sindhwani31-Oct-02 3:46 
GeneralRe: Unicode compilation Pin
Groda25-Apr-03 7:45
Groda25-Apr-03 7:45 
GeneralControls on the Dialog Pin
azmodius30-Aug-02 20:14
azmodius30-Aug-02 20:14 
GeneralRe: Controls on the Dialog Pin
hbb198127-Sep-03 23:15
hbb198127-Sep-03 23:15 
GeneralRe: Controls on the Dialog Pin
Rui Lopes28-Sep-03 3:41
Rui Lopes28-Sep-03 3:41 
GeneralPCX in 2 bit Pin
nileshwk8-Aug-02 5:11
nileshwk8-Aug-02 5:11 
GeneralRe: PCX in 2 bit Pin
Rui Lopes23-Aug-02 14:53
Rui Lopes23-Aug-02 14:53 
GeneralPremultiply Pin
Sibilant22-Jul-02 20:21
Sibilant22-Jul-02 20:21 
What does this do exactly?
void PreMultiplyRGBChannels(CBitmap &bmp, LPBYTE pBitmapBits)
{
BITMAP bmpInfo;
bmp.GetBitmap(&bmpInfo);

// pre-multiply rgb channels with alpha channel
for (int y=0; y
GeneralRe: Premultiply Pin
Rui Lopes23-Jul-02 0:18
Rui Lopes23-Jul-02 0:18 
GeneralRe: Premultiply Pin
Sibilant23-Jul-02 6:05
Sibilant23-Jul-02 6:05 
QuestionWin98/ME..? Pin
Mario M.15-Feb-02 6:28
Mario M.15-Feb-02 6:28 
AnswerRe: Win98/ME..? Pin
Rui Lopes15-Feb-02 9:02
Rui Lopes15-Feb-02 9:02 
GeneralPlatfrom SDK problem Pin
Janssens Johan10-Oct-01 13:47
Janssens Johan10-Oct-01 13:47 
GeneralRe: Platfrom SDK problem Pin
20-Jun-02 23:20
suss20-Jun-02 23:20 
GeneralPer Pixel Alpha w/ DirectDraw8 Pin
7-Jun-01 12:51
suss7-Jun-01 12:51 
QuestionUsing controls on the dialog? Pin
24-May-01 10:55
suss24-May-01 10:55 
AnswerRe: Using controls on the dialog? Pin
hbb198127-Sep-03 23:24
hbb198127-Sep-03 23:24 
GeneralRe: Using controls on the dialog? Pin
Rui Lopes28-Sep-03 3:38
Rui Lopes28-Sep-03 3:38 
GeneralDoes work in 16bpp but not in 32bpp Pin
Ramon Smits22-Apr-01 9:33
Ramon Smits22-Apr-01 9:33 
GeneralRe: Does work in 16bpp but not in 32bpp Pin
8-Jun-01 11:55
suss8-Jun-01 11:55 
GeneralWS_EX_LAYERED & SDK Pin
21-Apr-01 10:36
suss21-Apr-01 10:36 
GeneralRe: WS_EX_LAYERED & SDK Pin
Rui Lopes22-Apr-01 6:42
Rui Lopes22-Apr-01 6:42 
GeneralGreat ! I love it Pin
17-Apr-01 7:16
suss17-Apr-01 7:16 
QuestionWhy not add PNG support? Pin
Ramon Smits3-Apr-01 22:45
Ramon Smits3-Apr-01 22:45 
AnswerRe: Why not add PNG support? Pin
Rui Lopes4-Apr-01 0:26
Rui Lopes4-Apr-01 0:26 

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.