Click here to Skip to main content
Licence 
First Posted 11 Feb 2002
Views 321,370
Bookmarked 153 times

Per Pixel Alpha Blend in C#

By Rui Lopes | 3 Jun 2004
Demonstrates how to create per pixel alpha blending windows
2 votes, 4.0%
1
1 vote, 2.0%
2
2 votes, 4.0%
3
5 votes, 10.0%
4
40 votes, 80.0%
5
4.60/5 - 54 votes
3 removed
μ 4.23, σa 1.71 [?]

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 the new ways of doing the 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

31 May 2004

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

12 Feb 2002

  • Initial version.

Do you have any question or comment? Contact me!

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

About the Author

Rui Lopes

Web Developer

Portugal Portugal

Member


Sign Up to vote   Poor Excellent
Add a reason or comment to your vote: x
Votes of 3 or less require a comment

Comments and Discussions

 
You must Sign In to use this message board. (secure sign-in)
 
Search this forum  
 FAQ
    Noise  Layout  Per page   
  Refresh
GeneralWay to add some controls Pinmemberihess5:17 30 Jul '07  
GeneralRe: Way to add some controls [modified] PinmemberSp3cial9:12 24 Aug '08  
GeneralRe: Way to add some controls [modified] PinmemberLatencyXXX0:21 24 Jan '11  
GeneralHelp Pinmembergsampath21:15 28 May '07  
GeneralImage problem Pinmemberdarb213:57 20 Apr '07  
GeneralRe: Image problem PinmemberDaryl Fish15:58 15 Jan '08  
GeneralProblem when changing the form parent to desktop with dual-monitoring [modified] PinmemberPim-Pom2:59 7 Oct '06  
(Sorry for the poor English)
 
Hi,
I got a strange problem.
 
In my form i add something like :
[DllImport("user32.dll")]
private static extern int SetParent(IntPtr hWndChild, int hwndNewParent);

[DllImport("user32.dll", EntryPoint="FindWindowA")]
private static extern int FindWindow(string lpClassName, string lpWindowName);

private int GetDesktopWnd() {
return FindWindow("Progman", null);
}

private void button1_click(object sender, System.EventArgs e) {
SetParent(pngForm_.Handle, GetDesktopWnd);
}

 
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... Pinmembermoebiusproject10:35 12 Sep '06  
GeneralRe: Nice example, but... PinmemberTung Nguyen18:37 15 Apr '07  
GeneralRe: Nice example, but... Pinmembertic8414:14 10 Jun '08  
GeneralProblem in design mode PinmemberCapt'N Stabbin'20:09 12 Jul '06  
GeneralRe: Problem in design mode PinmemberODaniell1:52 3 Aug '06  
GeneralRe: Problem in design mode PinmemberMember 42028005:50 19 May '09  
QuestionHow Can I Add Controls? Pinmemberranzige17:32 2 Dec '05  
AnswerRe: How Can I Add Controls? Pinmemberwang.songqing21:16 20 Jun '06  
Generalrecognition Pinmemberamir mortazavi19:36 26 Jan '05  
QuestionCan this be done in DirectX? Pinmember#teve3:58 27 Dec '04  
GeneralNice but... Pinmemberrioman6:05 6 Sep '04  
GeneralRe: Nice but... PinmemberRui Godinho Lopes14:31 8 Sep '04  
GeneralRe: Nice but... Pinmemberrioman4:56 9 Sep '04  
GeneralRe: Nice but... PinsussAnonymous2:34 5 Oct '04  
GeneralRe: Nice but... Pinmembergreggman20:22 24 Jan '05  
GeneralRe: Nice but... [modified] PinmemberGreg Ellis7:45 20 Oct '06  
GeneralAlpha Blend as a background Pinmembergozza113:14 2 Jul '04  
Generalmulti transparent forms Pinmembermazro38:31 28 Jun '04  

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.

Permalink | Advertise | Privacy | Mobile
Web01 | 2.5.120210.1 | Last Updated 4 Jun 2004
Article Copyright 2002 by Rui Lopes
Everything else Copyright © CodeProject, 1999-2012
Terms of Use
Layout: fixed | fluid