Click here to Skip to main content
15,867,756 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 599.3K   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

 
GeneralAdd Controls PinPopular
Gottdrak9919-Nov-08 12:23
Gottdrak9919-Nov-08 12:23 
GeneralRe: Add Controls Pin
colche25-Feb-09 19:52
colche25-Feb-09 19:52 
GeneralRe: Add Controls Pin
rbrender9-Mar-09 17:44
rbrender9-Mar-09 17:44 
GeneralRe: Add Controls Pin
Marko Padjen31-Mar-09 13:39
Marko Padjen31-Mar-09 13:39 
GeneralRe: Add Controls Pin
LatencyXXX24-Mar-12 21:36
LatencyXXX24-Mar-12 21:36 
GeneralExactly what I was looking for. Thanks. Pin
mlavenne18-Aug-08 11:36
mlavenne18-Aug-08 11:36 
QuestionGreat but can I use the function also for a control? Pin
it-bergmann14-Feb-08 1:24
it-bergmann14-Feb-08 1:24 
AnswerRe: Great but can I use the function also for a control? Pin
sebastianlaskawiec19-May-09 4:49
sebastianlaskawiec19-May-09 4:49 
Generalcool Pin
radioman.lt12-Dec-07 22:20
radioman.lt12-Dec-07 22:20 
Questionwhy doing win32 with c#? Pin
bkaratte11-Dec-07 20:46
bkaratte11-Dec-07 20:46 
AnswerRe: why doing win32 with c#? Pin
tic8410-Jun-08 13:12
tic8410-Jun-08 13:12 
GeneralVB.Net Pin
dawmail33324-Aug-07 22:47
dawmail33324-Aug-07 22:47 
GeneralRe: VB.Net Pin
Dj Den4ik11-Feb-08 23:05
Dj Den4ik11-Feb-08 23:05 
GeneralRe: VB.Net Pin
Jason Newland25-Apr-12 19:31
Jason Newland25-Apr-12 19:31 
GeneralRe: VB.Net Pin
Jason Newland25-Apr-12 19:33
Jason Newland25-Apr-12 19:33 
GeneralGreat!!!!!!!!!!!!! Pin
shp-coding9-Aug-07 7:44
shp-coding9-Aug-07 7:44 
GeneralWay to add some controls Pin
ihess30-Jul-07 4:17
ihess30-Jul-07 4:17 
GeneralRe: Way to add some controls [modified] Pin
Sp3cial24-Aug-08 8:12
Sp3cial24-Aug-08 8:12 
GeneralRe: Way to add some controls [modified] Pin
LatencyXXX23-Jan-11 23:21
LatencyXXX23-Jan-11 23:21 
GeneralHelp Pin
Gihan Liyanage28-May-07 20:15
professionalGihan Liyanage28-May-07 20:15 
GeneralImage problem Pin
darb220-Apr-07 12:57
darb220-Apr-07 12:57 
GeneralRe: Image problem Pin
Daryl Fish15-Jan-08 14:58
Daryl Fish15-Jan-08 14:58 
GeneralProblem when changing the form parent to desktop with dual-monitoring [modified] Pin
Pim-Pom7-Oct-06 1:59
Pim-Pom7-Oct-06 1:59 
GeneralNice example, but... Pin
moebiusproject12-Sep-06 9:35
moebiusproject12-Sep-06 9:35 
GeneralRe: Nice example, but... Pin
Tung Nguyen15-Apr-07 17:37
Tung Nguyen15-Apr-07 17:37 

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.