Click here to Skip to main content
15,885,757 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 601.8K   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

 
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 
(Sorry for the poor English)

Hi,
I got a strange problem.

In my form i add something like :
[DllImport("user32.dll")]<br />
private static extern int SetParent(IntPtr hWndChild, int hwndNewParent);<br />
<br />
[DllImport("user32.dll", EntryPoint="FindWindowA")]<br />
private static extern int FindWindow(string lpClassName, string lpWindowName);<br />
<br />
private int GetDesktopWnd() {<br />
    return FindWindow("Progman", null);<br />
}<br />
<br />
private void button1_click(object sender, System.EventArgs e) {<br />
    SetParent(pngForm_.Handle, GetDesktopWnd);<br />
}


So when I click button1 my pngForm_ is now drawed on the desktop.

This worked great with 1 monitor.. But now I'm dual-monitoring and I got a bug when I try to move the pngForm_ when it's on the desktop. By example I hit the form when it's on monitor 1, and the form go on monitor 2 at the same position, and after it's impossible to move it.

I have made a lot of test, and i thinks it's due to the cp.ExStyle. In Fact when I remove the WS_EX_LAYERED it works..

Any idea to repair that ?


-- modified at 8:51 Saturday 7th October, 2006
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 
GeneralRe: Nice example, but... Pin
tic8410-Jun-08 13:14
tic8410-Jun-08 13:14 
GeneralProblem in design mode Pin
Member 303363812-Jul-06 19:09
Member 303363812-Jul-06 19:09 
GeneralRe: Problem in design mode Pin
ODaniell3-Aug-06 0:52
ODaniell3-Aug-06 0:52 
GeneralRe: Problem in design mode Pin
sebastianlaskawiec19-May-09 4:50
sebastianlaskawiec19-May-09 4:50 
QuestionHow Can I Add Controls? Pin
ranzige2-Dec-05 16:32
ranzige2-Dec-05 16:32 
AnswerRe: How Can I Add Controls? Pin
wang.songqing20-Jun-06 20:16
wang.songqing20-Jun-06 20:16 
Generalrecognition Pin
amir mortazavi26-Jan-05 18:36
amir mortazavi26-Jan-05 18:36 
QuestionCan this be done in DirectX? Pin
#teve27-Dec-04 2:58
#teve27-Dec-04 2:58 
GeneralNice but... Pin
rioman6-Sep-04 5:05
rioman6-Sep-04 5:05 
GeneralRe: Nice but... Pin
Rui Lopes8-Sep-04 13:31
Rui Lopes8-Sep-04 13:31 
GeneralRe: Nice but... Pin
rioman9-Sep-04 3:56
rioman9-Sep-04 3:56 
GeneralRe: Nice but... Pin
Anonymous5-Oct-04 1:34
Anonymous5-Oct-04 1:34 
GeneralRe: Nice but... Pin
greggman24-Jan-05 19:22
greggman24-Jan-05 19:22 
GeneralRe: Nice but... [modified] Pin
Greg Ellis20-Oct-06 6:45
Greg Ellis20-Oct-06 6:45 
GeneralAlpha Blend as a background Pin
gozza112-Jul-04 2:14
gozza112-Jul-04 2:14 

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.