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.

31 May 2004

12 Feb 2002

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.

Comments and Discussions

I'm having trouble displaying the transparent image to a set destination, it seems to only want to draw within a certain area of the screen. Is the PerPixelAlphaForm being set to a certain dimension somewhere I can edit?

Great job, it looks great! I was just wondering, how would I go about drawing shapes such as rectangles, ellipses and lines using GDI? I've tried creating a Graphics object with the Graphics.FromHdc method but when I try drawing something, it doesn't appear.

Hi, I wanted to use this code inside an MDI Container to darken the area around an dialog box (like Vista/7 does), but I can't really figure out how to do it.
When I try to make the image bigger than it's 'physical' size is, it just isn't displayed. Do you know how to solve this prob? I haven't found a solution so far.

I made an image in the GIMP and saved it as a transparent png but it isn't working?
I'm getting the "The bitmap must be 32ppp with alpha-channel." exception. How do I check to this? I've never hear of "ppp" before.

-Elmernite

After editing around I found that I need to set "Save color values from transparent pixels" With that checked it works.