Click here to Skip to main content
Licence 
First Posted 11 Feb 2002
Views 330,540
Bookmarked 156 times

Per Pixel Alpha Blend in C#

By | 3 Jun 2004 | Article
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 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
QuestionCan this be done in DirectX? Pinmember#teve2:58 27 Dec '04  
GeneralNice but... Pinmemberrioman5:05 6 Sep '04  
GeneralRe: Nice but... PinmemberRui Godinho Lopes13:31 8 Sep '04  
GeneralRe: Nice but... Pinmemberrioman3:56 9 Sep '04  
GeneralRe: Nice but... PinsussAnonymous1:34 5 Oct '04  
GeneralRe: Nice but... Pinmembergreggman19:22 24 Jan '05  
GeneralRe: Nice but... [modified] PinmemberGreg Ellis6:45 20 Oct '06  
Ya I'll tell ya how to do it.
 
Create a form based on the perpixelalphablended deal and load it up with your background png.
 
Next create another dummy form. Make this form the exact same size as the perpixelalpha form. Add all the controls that you want on top of this dummy form.
 
Now, in the contstructor of your dummy form create a region that specifies all the areas where you controls are and set it to the form like shown below.
 

public Form1()
{
System.Drawing.Drawing2D.GraphicsPath p = new System.Drawing.Drawing2D.GraphicsPath();
 
p.AddRectangle(new Rectangle(0,0, 803, 24)); //my own title bar
p.AddRectangle(new Rectangle(53, 122, 60, 40));// a button on my form
 
this.Region = new Region(p);
}

 
OR the other way... NOT using Regions. Make your dummy form a nice color like 255,0,255 and then set the transparency key to 255,0,255 also. This will work better than regions actually, because when you move your form you wont see the ugly dialog background color behind the controls.
 
Then position your dummy form exactly over top of your perpixel form. Then write some funky code to move each form at the same time when dragged.
 
If someone has a better way to it (I doubt it), then I CHALLENGE you to post the code here.
 
To move the windows simultaneously was easier than I thought. Gotter going perfectly. Keep checking www.clubdjpro.com for the DJRipper. Then you will see what it is capable of.
 
Then yer all good.
Cheers,
Greg
 

 

-- modified at 12:50 Tuesday 24th October, 2006
GeneralAlpha Blend as a background Pinmembergozza112:14 2 Jul '04  
Generalmulti transparent forms Pinmembermazro37:31 28 Jun '04  
GeneralCreating Slices of an Image PinmemberSameers (theAngrycodeR )5:55 9 Jun '04  
GeneralClipping Problem Pinmembergarythemann11:51 5 Apr '04  
GeneralRe: Clipping Problem PinmemberTom Guinther13:00 7 May '04  
GeneralRe: Clipping Problem Pinmemberj2demelo17:30 30 May '04  
GeneralRe: Clipping Problem PinmemberRui Godinho Lopes1:51 31 May '04  
QuestionCan not Hit it at Lasting Windows XP Updata & Windows 2003. Pinmemberwujingping5:21 2 Apr '04  
AnswerRe: Can not Hit it at Lasting Windows XP Updata & Windows 2003. PinmemberTom Guinther2:48 8 Jun '04  
GeneralUsed your code Pinmemberpardue7:40 24 Mar '04  
QuestionVB.NET Example? PinmemberandrewPP11:23 6 Dec '03  
AnswerRe: VB.NET Example? PinmemberSteve Schaneville11:13 23 Dec '03  
GeneralRe: VB.NET Example? Pinmemberthesleeper21:17 19 Jul '04  
GeneralNice but superfluous Pinmemberbloodyduke7:15 2 Sep '03  
GeneralRe: Nice but superfluous PinmemberRui Godinho Lopes7:49 2 Sep '03  
GeneralRe: Nice but superfluous PinmemberWackatronic0:34 4 Jun '04  
GeneralWin2K Bug Pinmembernothanks198420:39 13 Aug '03  
GeneralRe: Win2K Bug PinmemberRui Godinho Lopes0:30 14 Aug '03  

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
Web04 | 2.5.120529.1 | Last Updated 4 Jun 2004
Article Copyright 2002 by Rui Lopes
Everything else Copyright © CodeProject, 1999-2012
Terms of Use
Layout: fixed | fluid