1

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?

Glen
  • 655
  • 3
  • 16
  • 31

1 Answers1

0

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

Stephan Zaria
  • 496
  • 5
  • 12
  • are you sure there isn't a simpler solution? I don't get why it doesn't just render the PNG as it should. I have even tried using the image in a picture box over the transparent form, but it has the same problem – Glen Jan 06 '14 at 11:44
  • Unfortunately, I'm sure. The problem is that the last time the GDI technology was updated, was at the release of Windows XP - way before png's became a widely used format for GUI. WPF should support semi-transparent windows, but as long as you are using WinForms (and GDI, by extent), your only options are to either choose one transparent color or use a bit-mask. – Stephan Zaria Jan 06 '14 at 12:18