Dev-Picayune

picayune: of little value or importance

.NET Shaped Windows mystery solved in 2 ways

Last night I had the pleasant surprise of finding that an issue from .net v1 and 1.1 with shaped windows has been resolved in the VS 2005 and .NET 2 Beta. If you do a search for “Shaped Windows C#.NET” or something like that, you’ll get tons of hits referencing the concept of setting the form’s transparency key to a color that you want to make transparent. If you fill a window (with no frame) with that color and then have few bits of graphics or objects that are not that color, it will draw an irregular shaped window with only the items you want on there. Unfortunately, if you had your color settings greater than 16-bits (which any self-respecting photographer would), it won’t work anymore. The color that should have been transparent shows up.

But in attempting to do something related for work today, I stumbled across these magical 4 lines of code that allow you to implement irregular shaped windows in 32-bit color mode.

System.Drawing.Bitmap Img = new System.Drawing.Bitmap(this.BackgroundImage);Img.MakeTransparent(Img.GetPixel(0,0));this.BackgroundImage = Img;this.TransparencyKey = Img.GetPixel(0,0);

The ‘this.BackgroundImage’ is my addition so that you can still statically compile the image resource into your project. One thing to watch out for though is that this assumes that position 0,0 holds a pixel with the color you want to make transparent. What this does, is opens up the posibility of completing my Nevada project the way I have always wanted to do it.

No comments yet. Be the first.

Leave a reply

For spam filtering purposes, please copy the number 9584 to the field below: