Click here to Skip to main content
15,888,124 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 602.7K   19.4K   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

 
GeneralCreating Slices of an Image Pin
Sameers Javed9-Jun-04 5:55
Sameers Javed9-Jun-04 5:55 
GeneralClipping Problem Pin
Gary Mann5-Apr-04 11:51
Gary Mann5-Apr-04 11:51 
GeneralRe: Clipping Problem Pin
Tom Guinther7-May-04 13:00
Tom Guinther7-May-04 13:00 
GeneralRe: Clipping Problem Pin
j2demelo30-May-04 17:30
j2demelo30-May-04 17:30 
GeneralRe: Clipping Problem Pin
Rui Lopes31-May-04 1:51
Rui Lopes31-May-04 1:51 
QuestionCan not Hit it at Lasting Windows XP Updata & Windows 2003. Pin
wujingping2-Apr-04 5:21
wujingping2-Apr-04 5:21 
AnswerRe: Can not Hit it at Lasting Windows XP Updata & Windows 2003. Pin
Tom Guinther8-Jun-04 2:48
Tom Guinther8-Jun-04 2:48 
GeneralUsed your code Pin
joepardue24-Mar-04 7:40
joepardue24-Mar-04 7:40 
Rui,

I used your code as the basis for a per pixel alpha demo in my tutorial at:

http://www.codeproject.com/cs/media/CsTranspTutorial3.asp

I was fascinated by your demo, but I didn't know enough to make any use of it, so I did a study that lead to my three transparency tutorials, and got to where I think I understand what you are doing just enough to be dangerous.

The per pixel alpha transparency demo comes up with a bottle of Aqua Mala water (guaranteed 90% pure) that can be dragged around the desktop and has a mouse sensitive 'button'. I really got a kick out of programming this little demo.

Thanks for getting me started in transparency.

Joe

QuestionVB.NET Example? Pin
andrew|6-Dec-03 11:23
andrew|6-Dec-03 11:23 
AnswerRe: VB.NET Example? Pin
Steve Schaneville23-Dec-03 11:13
professionalSteve Schaneville23-Dec-03 11:13 
GeneralRe: VB.NET Example? Pin
thesleeper19-Jul-04 21:17
thesleeper19-Jul-04 21:17 
GeneralNice but superfluous Pin
lsauer2-Sep-03 7:15
lsauer2-Sep-03 7:15 
GeneralRe: Nice but superfluous Pin
Rui Lopes2-Sep-03 7:49
Rui Lopes2-Sep-03 7:49 
GeneralRe: Nice but superfluous Pin
Wackatronic4-Jun-04 0:34
Wackatronic4-Jun-04 0:34 
GeneralWin2K Bug Pin
nothanks198413-Aug-03 20:39
nothanks198413-Aug-03 20:39 
GeneralRe: Win2K Bug Pin
Rui Lopes14-Aug-03 0:30
Rui Lopes14-Aug-03 0:30 
GeneralRe: Win2K Bug Pin
Vuvirt22-Oct-03 23:10
Vuvirt22-Oct-03 23:10 
GeneralRe: Win2K Bug Pin
j2demelo30-May-04 17:15
j2demelo30-May-04 17:15 
GeneralRe: Win2K Bug Pin
j2demelo30-May-04 18:09
j2demelo30-May-04 18:09 
QuestionWithout PNG? Pin
Vuvirt22-Apr-03 2:21
Vuvirt22-Apr-03 2:21 
AnswerRe: Without PNG? Pin
Anonymous25-Apr-03 18:32
Anonymous25-Apr-03 18:32 
AnswerRe: Without PNG? Pin
Rui Lopes27-Apr-03 0:42
Rui Lopes27-Apr-03 0:42 
GeneralRe: Without PNG? Pin
Vuvirt28-Apr-03 19:58
Vuvirt28-Apr-03 19:58 
QuestionControls can be alpha blended? Pin
Dixion17-Sep-02 14:21
Dixion17-Sep-02 14:21 
AnswerRe: Controls can be alpha blended? Pin
Eric Hansen2-Oct-03 14:04
Eric Hansen2-Oct-03 14: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.