PNG image have transparent parts and I want this image as background for my form.
I've set ControlStyles.SupportsTransparentBackColor
to true
and used this.BackColor = Color.Transparent;
but this is just not working at all, the back color appears solid gray when im starting app. Even if I set BackColor to Transparent in Properties->Appearence. I can see it Transparent in form design preview but it still appears solid gray when Im start the application.
Playing with TransparencyKey
also gaves me a bad results - the half-transparent pixels of PNG image became colored with TransparencyKey
color and i can't use Transparent color for TransparencyKey
. Looking for some help.
EXAMPLE:
https://i.stack.imgur.com/u9p6N.jpg

- 61
- 1
- 11
-
Have you seen answer here at this link http://stackoverflow.com/questions/4314215/c-sharp-transparent-form – Hassan May 23 '14 at 19:47
-
iv'e added image to the question, so you can see the problem with TransparencyKey – SavageStyle May 23 '14 at 20:05
-
1You need per-pixel alpha transparency, not directly supported in Winforms because it is incompatible with lots of controls in the toolbox. The kind that are implemented by Windows, they get transparent as well. Using a WPF window is the logical approach. – Hans Passant May 24 '14 at 22:50
1 Answers
You may use the TransparencyKey
incorrectly. The idea behind this property is that the color specified as the TransparencyKey
will be transparent. E.g. if you set myForm.TransparencyKey = Color.Red
anything that is red will be transparent. However, if the shade of red is different than the one described as Color.Red
it won't be transparent.
Or, to put it another way, it will result in a transparent background:
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
this.BackColor = Color.FromArgb(100, 0, 0);
this.TransparencyKey = Color.FromArgb(100, 0, 0);
}
}
But this will not be transparent:
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
this.BackColor = Color.FromArgb(99, 0, 0);
this.TransparencyKey = Color.FromArgb(100, 0, 0);
}
}
So now if your image has white background and you set this.TransparencyKey = Color.White
only white parts of the image will be transparent. But if there are any grayish, white-ish areas you will see this color instead of transparent background.
If that's the case you may need to edit the image to be sure the background is everywhere of the same color.

- 8,408
- 6
- 48
- 68