Click here to Skip to main content
15,902,299 members
Articles / Programming Languages / C#

Per Pixel Alpha Blend in C#

Rate me:
Please Sign up or sign in to vote.
4.72/5 (73 votes)
3 Jun 20041 min read 606.4K   19.5K   191   115
Demonstrates how to create per pixel alpha blending Windows

Preview of the per pixel alpha blend effect

Preview of the per pixel alpha blend effect

Introduction

This is a port of my other article from C++/MFC to C#/Windows Forms. The concept of creating a per-pixel-alpha blending window remains the same, the GDI function UpdateLayeredWindow.

What Changed

  • Image loading is handled by GDI+ classes (System.Drawing namespace)
  • No need to pre-multiply the rgb channels with the alpha channel
  • No more support for PSP files. PNG file are the best choice

Usage

To use this code, you need to include PerPixelAlphaForm.cs file in your project, create a class that inherits from PerPixelAlphaForm, load a bitmap using System.DrawingImage.FromFile static method and then call the PerPixelAlphaForm.SetBitmap method.

The example source code is inside the main.cs file. In the source code, you will also find new ways of doing old things, like, handling dropped files from Windows Explorer.

 

Installing & Building

  1. Download and extract the source code of this article.
  2. To build, run the build.bat file from command prompt.
  3. Now, run the bin\main.exe and enjoy!

Changelog

31st May, 2004

  • Fix clipping problems by creating the form without borders.
  • Move PerPixelAlphaForm class to PerPixelAlphaForm.cs file.

12th February, 2002

  • Initial version

Do you have any question or comment? Please leave a note below.

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
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

 
GeneralRe: Can't add controls? Pin
Tom Guinther8-Jun-04 3:02
Tom Guinther8-Jun-04 3:02 
GeneralRe: Can't add controls? Pin
BryanCallahan16-Jun-04 18:56
BryanCallahan16-Jun-04 18:56 
GeneralRe: Can't add controls? Pin
Tom Guinther17-Jun-04 3:50
Tom Guinther17-Jun-04 3:50 
AnswerRe: Can't add controls? Pin
Michael Mondry8-Feb-04 8:48
Michael Mondry8-Feb-04 8:48 
AnswerRe: Can't add controls? Pin
BryanCallahan16-Jun-04 18:50
BryanCallahan16-Jun-04 18:50 
GeneralRe: Can't add controls? Pin
andrew|10-Dec-04 9:25
andrew|10-Dec-04 9:25 
AnswerRe: Can't add controls? Pin
Tcherun31-Oct-05 22:15
Tcherun31-Oct-05 22:15 
AnswerRe: Can't add controls? Pin
royconejo24-Jan-06 17:59
royconejo24-Jan-06 17:59 
So far (and as has been said before) with UpdateLayeredWindow your window will look like the bitmap you pass to that function. PERIOD.

So, how to draw controls? I figure out that my onwer drawn buttons were still receiving messages (even OnDraw) in response to their custom mouseover or pressed status, so my approach was to draw them on the window bitmap (do not draw on the DC passed with OnDraw, that will draw nothing) and then UpdateLayeredWindow to reflect changes (everytime there was a change, of course).

This approach is very easy to implement and works great. I've derived another controls starting from an owner draw CButton (checkboxes, etc) and the static controls were drawn before-hand on the bitmap. It works for a dialog with only some buttons, you can even create custom controls derived from a owner draw CButton or display images easily.

If you want a fully functional GUI, well... having in mind that you are basically drawing everything to a framebuffer and that you always have to update the whole window, the best option might be to design a new custom GUI with their own controls, or use something like PicoGUI. Whatever you do, do not forget that the point here is to have a very nice looking window alpha blended per-pixel with the desktop, not to reinvent a GUI inside another GUI Wink | ;)

Hope this helps ^^
QuestionRe: Can't add controls? Pin
Assaf Koren21-Apr-06 19:16
Assaf Koren21-Apr-06 19:16 
GeneralRe: Can't add controls? Pin
Member 278195916-Oct-08 18:09
Member 278195916-Oct-08 18:09 
GeneralCFileDialog Pin
24-May-02 20:42
suss24-May-02 20:42 
GeneralRe: CFileDialog Pin
Jared Bienz8-Jul-02 8:23
Jared Bienz8-Jul-02 8:23 
QuestionCan portions of a window be Alpha Blended Pin
19-Feb-02 10:39
suss19-Feb-02 10:39 
AnswerRe: Can portions of a window be Alpha Blended Pin
Rui Lopes20-Feb-02 21:58
Rui Lopes20-Feb-02 21:58 
QuestionWin98/ME...? Pin
Mario M.15-Feb-02 6:27
Mario M.15-Feb-02 6:27 
AnswerRe: Win98/ME...? Pin
Rui Lopes15-Feb-02 9:09
Rui Lopes15-Feb-02 9:09 
GeneralRe: Win98/ME...? Pin
Mario M.15-Feb-02 9:36
Mario M.15-Feb-02 9:36 
GeneralRe: Win98/ME...? Pin
Rui Lopes15-Feb-02 10:59
Rui Lopes15-Feb-02 10:59 
GeneralRe: Win98/ME...? Pin
Mario M.15-Feb-02 12:59
Mario M.15-Feb-02 12:59 
GeneralRe: Win98/ME...? Pin
Rui Lopes16-Feb-02 9:14
Rui Lopes16-Feb-02 9:14 
GeneralNice stuff Pin
Patrick Hoffmann13-Feb-02 22:42
Patrick Hoffmann13-Feb-02 22:42 

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.