How to make transparent buttons in C#
I make my own buttons with pictureBox. They should be round and drag only, and is fine to look to.
But I am unable to transparent device to work. I use MakeTrasparent to make a color transparent. The problem is that color is not transparent, but the same as the background color of the form. Is there something wrong, or have I just misunderstood?
Code:
Bitmap b = new Bitmap (recorderButtonPic.Image);
Color t = b.GetPixel (1, 1);
b.MakeTransparent (t);
recorderButtonPic.Image = b;
Re: How to make transparent buttons in C#
Do not set this property on Bitmap. Just when you make a Bitmap without an Alpha channel, select a color mask.
Use Form.TransparencyKey instead.
Re: How to make transparent buttons in C#
Is it the way you think:
Code:
Bitmap b = new Bitmap (recorderButtonPic.Image);
b.MakeTransparent (TPC.ActiveForm.TransparencyKey);
recorderButtonPic.Image = b;
Isn't that the same.
Re: How to make transparent buttons in C#
You made a mistake my friend. This will give an error.
Code:
TCP.ActiveForm.TransparencyKey = Color.Magenta
Afterwards, you can draw the shape, just set Image property of the Bitmap you have made.
You can change TCP.ActiveForm.TransparencyKey to [Bitmap].GetPixel (0, 0) if you wish.
MakeTransparent is to set a transparent color on a bitmap, that you do not need to do in this case.
Re: How to make transparent buttons in C#
So is that correct now:
Code:
Form.ActiveForm.TransparencyKey = Form.ActiveForm.BackColor;
Unfortunately it gives an exception:
Quote:
Cross-thread operation not valid: Control '' accessed from a thread other than the thread it was created on.
Re: How to make transparent buttons in C#
Choose the form in the editor, and select TransparencyKey property, and select Color.Control instead. If that does not help to put PictureBox.BackColor to Color.Transparent, you must declare a separate class:
Code:
class MyTransparentPictureBox : PictureBox
{
public override CreateParams CreateParams
{
get {CreateParam param = base.CreateParams; param.ExStyle | = 0x00000020; return param;}
}
}