Transparent PNG In Picture Box

2.6k Views Asked by At

I am a little baffled here. In C# I have my form which has a transparent background, with a picturebox on the form. The picturebox has a PNG image with a lot of transparent areas, and this is clear as i have looked in photoshop...the transparency is there!

However, when i run my application the PNG image is for some reason ignoring the transparent parts? Any ideas?

Right now the image looks like it has tried to do the transparency on some parts, but its made a horrible blotchy looking image.

I need to explain this with a picture:

enter image description here

On the left you can see i have set the form background to the blue, and set no transparancy key. The interface PNG over the top with the transparency between the interface elements and even the interface semi-transparent background works! You can see the form background through the image.

However, on the right image i have set the forms transparency key to the same as the background colour...it has made all of the form transparent but it seems to be ignoring the transparent parts between the interface items on the PNG...making it look like the form background colour is still under the picture box and making that awful jagged edge.

So if the PNG works wth the form not being transparent, why does it not hide the rest of the form under the PNG ? All i should see here is the desktop showing through between the PNG interface elements and through the semi-transparent parts...but all i see is the colour i set on the form!

I hope that made sense.

i have since worked out it only happens if parts of the png have drop shadows?

1

There are 1 best solutions below

2
On

This happens, because, as you've said, you are using drop shadows. The transparency key works by removing the exact color you've specified from the display, after the form is drawn, so when the shadows get mixed with the background, they create a new color that isn't transparent. You can bypass this by using some API calls, and creating a per-pixel mask (basically a grayscale bitmap, telling each pixel what it's transparency level should be).

There is a good example project here