Click here to Skip to main content
15,889,992 members
Articles / Desktop Programming / MFC

Using the AlphaBlend Function

Rate me:
Please Sign up or sign in to vote.
4.43/5 (19 votes)
1 Feb 2000 341.3K   7.4K   49   67
A sample application that uses the AlphaBlend function to produce a transparent splash screen

Sample Image - alphablend.gif

Note: The code in this article is restricted to win98 and win2000.

Do you want a cool splash window? Do you think the general splash window is not good enough for your app? Then this sample source is for you.

My semi-transparent splash window uses the AlphaBlend function. When I found this function, I was so surprised! The function works just like BitBlt for bitmaps that have transparent or semitransparent pixels. Here is the AlphaBlend function's syntax.

C++
BOOL AlphaBlend( HDC hdcDest,
  int nXOriginDest,
  int nYOriginDest,
  int nWidthDest,
  int nHeightDest,
  HDC hdcSrc,
  int nXOriginSrc,
  int nYOriginSrc,
  int nWidthSrc,
  int nHeightSrc,
  BLENDFUNCTION blendFunction
);

The important parameter is the last one, BLENDFUNCTION.

C++
typedef struct _BLENDFUNCTION {
    BYTE     BlendOp;
    BYTE     BlendFlags;
    BYTE     SourceConstantAlpha;
    BYTE     AlphaFormat;
}BLENDFUNCTION;
  • BlendOp must be AC_SRC_OVER
  • BlendFlags must be 0
  • SourceConstantAlpha must be between 0 (transparent) and 255 (semi-transparent)
  • AlphaFormat must be AC_SRC_ALPHA

MSDN states: The SourceConstantaAlpha member of BLENDFUNCTION specifies an alpha transparency value to be used on the entire source bitmap. The SourceConstantAlpha value is combined with any per-pixel alpha values. If you set SourceConstantAlpha to 0, it is assumed that your image is transparent. Set the SourceConstantAlpha value to 255 (indicates that the image is opaque) when you only want to use per-pixel alpha values.

See the sample application for a demonstration.

Just enjoy!!

Ajou University C.C. 4th member.

License

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
Software Developer (Senior)
Korea (Republic of) Korea (Republic of)
Woo Seok Seo have been a Microsoft MVP for 7 years and have translated several books into Korean. Author of C# Programming for Beginner (DevPress, 2001), he is interested in Debugging techniques and .NET technology. Get in touch with Woo Seok Seo at wooseok.seo@gmail.com

Comments and Discussions

 
QuestionGotcha with 64-bit OS and AlphaBlend Pin
Snuff Daddy12-Jun-15 12:33
Snuff Daddy12-Jun-15 12:33 
QuestionAlphaBlend mode using Borland C++ 5.0 Pin
Member 1091480330-Jun-14 8:33
Member 1091480330-Jun-14 8:33 
GeneralMy vote of 3 Pin
Mahdi Nejadsahebi30-Apr-11 21:24
Mahdi Nejadsahebi30-Apr-11 21:24 
GeneralMy vote of 2 Pin
Anaghir6-Jan-10 0:41
Anaghir6-Jan-10 0:41 
QuestionAlphaBlend draws over the existing backgruound making it darker. gradually losing transaprency Pin
VC++Maniac20-Apr-09 21:15
VC++Maniac20-Apr-09 21:15 
AnswerRe: AlphaBlend draws over the existing backgruound making it darker. gradually losing transaprency Pin
Snuff Daddy12-Jun-15 12:41
Snuff Daddy12-Jun-15 12:41 
GeneralAbout OnPaint with the AlphaBlend(); [modified] Pin
Ted Lin16-Aug-07 22:12
Ted Lin16-Aug-07 22:12 
GeneralAlphaBlend wrong formula ?! Pin
Octopod23-Sep-06 10:17
Octopod23-Sep-06 10:17 
GeneralRe: AlphaBlend wrong formula ?! Pin
Octopod23-Sep-06 10:50
Octopod23-Sep-06 10:50 
GeneralThank you Pin
Nick Z.23-May-05 10:41
Nick Z.23-May-05 10:41 
GeneralProblem with AlphaBlend Pin
Petru Dumitrache23-Mar-05 23:13
Petru Dumitrache23-Mar-05 23:13 
GeneralRe: Problem with AlphaBlend Pin
jgarnett27-Oct-05 15:59
jgarnett27-Oct-05 15:59 
GeneralRe: Problem with AlphaBlend Pin
Snuff Daddy12-Jun-15 12:31
Snuff Daddy12-Jun-15 12:31 
GeneralQuestion Pin
one_eddie1-Oct-04 11:40
one_eddie1-Oct-04 11:40 
GeneralGreat stuff! Pin
JustDan24-Jan-04 17:36
JustDan24-Jan-04 17:36 
GeneralRe: Great stuff! Pin
anothermxw4-Apr-04 19:08
anothermxw4-Apr-04 19:08 
GeneralRe: Great stuff! Pin
Anonymous5-Apr-04 14:18
Anonymous5-Apr-04 14:18 
GeneralNo with Dialog Pin
Person_17-May-03 5:42
Person_17-May-03 5:42 
GeneralAlgorithm Code Pin
27-Feb-03 17:34
suss27-Feb-03 17:34 
GeneralRe: Algorithm Code Pin
Henry P.27-Feb-03 17:57
Henry P.27-Feb-03 17:57 
GeneralRe: Algorithm Code Pin
Jack Puppy28-Feb-03 19:38
Jack Puppy28-Feb-03 19:38 
GeneralRe: Algorithm Code Pin
Anonymous3-Mar-03 6:27
Anonymous3-Mar-03 6:27 
GeneralRe: Algorithm Code Pin
Jack Puppy8-Mar-03 4:50
Jack Puppy8-Mar-03 4:50 
GeneralRe: Algorithm Code Pin
Henry P.31-Mar-03 19:22
Henry P.31-Mar-03 19:22 
I've created an SDI project, and paste the code you provide into the CMyView::OnDraw function. I changed the color of the memDC to red to make the painting more easily observable. I did not do a formal timing test, but by resizing the view, the repainting occurs much quicker with the Windows AlphaBlend as compared to Christian's AlphaBlendU. The former repainting is almost instanstaneous whereas the latter pauses (the background takes on the original white color for a split second) before the painting is completed.

This test was done on a AMD 1.8GHz with WindowsXP, 512MB RAM. This could be a system-specific finding.

The Windows AlphaBlend function actually allows two different blending modes, by specifying the last parameter (AlphaFormat) of the BLENDFUNCTION struct. For reference:

typedef struct _BLENDFUNCTION
{
BYTE BlendOp;
BYTE BlendFlags;
BYTE SourceConstantAlpha;
BYTE AlphaFormat;
}BLENDFUNCTION, *PBLENDFUNCTION, *LPBLENDFUNCTION;

If AlphaFormat is 0, the source image is assumed to have a constant alpha as specified by SourceConstantAlpha. If AlphaFormat is AC_SRC_ALPHA, the source image is assumed to contain individual alpha values for each pixel (the alpha value being contained in the fourth byte of each pixel in a 32-bit (4 bytes per pixel) bitmap.

The reason I bring this up is because I've found that, at least for several computer systems I've tried on (both AMD, WinXP and Win2000), the blending is SIGNIFICANTLY faster when AlphaFormat is 0 (comparable to a BitBlt of the same bitmap size), whereas when the AlphaFormat is AC_SRC_ALPHA, the speed is slower, comparable to Christian's algorithm.

My speculation is that for the case where AlphaFormat is 0 (i.e., only a single SourceConstantAlpha is used), the Windows function does not need to extract the device-independent bits, and rather operate directly on the device-dependent bitmaps. The Windows function may also take advantage of driver acceleration as well...but this is just speculation.

If anyone discovers other interesting findings, please post them here.


Henry P.
http://www.allersoft.com

GeneralRe: Algorithm Code Pin
B2kguga25-Feb-07 10:04
B2kguga25-Feb-07 10:04 

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.