I am trying to create a window that has a cool flaming icon sticking out with transparency around it. I’ve read multiple threads here on this website stating that I should do it a specific way, and I’ve done so. However I am getting a very, very weird result.
Firstly, this is how it should look (please ignore the Photoshop transparent background):
But it looks like this:
This is the code I’ve used:
this.TransparencyKey = Color.Orange;
this.BackColor = Color.Brown;
InitializeComponent();
I hope someone can help me:)
EDIT:
Like you can see, it has orange pixelated stuff around the flames (changes according to the color I put)
3
Answers
You’re using a transparency key, which is an all-or nothing solution: each pixel is either fully transparent or fully opaque. That’s why you get this result.
You need alpha-blended transparency for your form.
I’ve found a couple articles that may be helpful.
TransparencyKey
does no alpha blending. It just sets a single colour as completely transparent; the rest is completely opaque. There are numerous tutorials around the web how to do proper alpha blending with arbitrary window shapes, e.g. here.Note that WPF is significantly easier to work with here. If a window’s background is partially-transparent, then that’s what you get. No need to mess around with WinAPI calls.
I have done something like this 10 years ago. For example:
Note however this is not semi-transparent, but alpha transparent with semi-transparent background. Your problem is that you set transparency to be single color and assume it will be alpha transparent. That is why you see crusty pixels.
There are meany ways to create this effect, but some are way too complex. Here is how to do this in c++ (link). If you wonder why this is relevant then this is because you need to use Windows API directly or indirectly and it at least describes what you need to use.
Other easy ways to do this is to take a screenshot of background and draw it and then draw on top of it. Note that if window is on top of a video for fast changing background then visual appearance might suffer.