user1207217
user1207217

Reputation: 557

C# transparent background for window form

I've already seen Transparent background on winforms?

it doesnt offer solution to my problem. I am using the same method to try to achieve transparency

    public Form1()
    {
        this.SetStyle(ControlStyles.SupportsTransparentBackColor, true);
        InitializeComponent();
        this.BackColor = Color.FromArgb(0, 0, 0, 0);
    }

But this gives a grey background, not transparent. How can I get an actually transparent background (note, transparency key solutions do not give a transparent background, and when I paint with alpha channel less than 255, it blends with the set form background colour, and not the actual background)? I want to paint to certain regions of the screen with alpha < 255 and blend with the background (not the form).

Upvotes: 6

Views: 35802

Answers (4)

Pameela
Pameela

Reputation: 1

public partial class TransprtScrcn : Form
    {
        public TransprtScrcn()
        {
            InitializeComponent();
            this.BackColor = Color.Red;
            this.TransparencyKey = Color.Red;
        }


        protected override void OnPaintBackground(PaintEventArgs e)
        {
            e.Graphics.FillRectangle(Brushes.Red, e.ClipRectangle);
        }
    }
}

Upvotes: -1

ahmad mirza
ahmad mirza

Reputation: 35

This is the best way to make the transparent background of winform.

right after this:

public partial class frmTransparentBackcolor : Form
{
    public frmTransparentBackcolor()
    {
        InitializeComponent();
        //set the backcolor and transparencykey on same color.
        this.BackColor = Color.LimeGreen;
        this.TransparencyKey = Color.LimeGreen;
    }
    protected override void OnPaintBackground(PaintEventArgs e)
    {
        e.Graphics.FillRectangle(Brushes.LimeGreen, e.ClipRectangle);
    }
}

hope this will help.

Upvotes: 3

a d
a d

Reputation: 572

You can use a picture for this work. The color of bound of picture is Red , Next use this code

this.TransparencyKey = Color.Red;

Upvotes: 0

Tommaso Belluzzo
Tommaso Belluzzo

Reputation: 23685

The way I did it long time ago was to find an unused color for the form background and then set the transparency key to it:

this.BackColor = Color.Magenta;
this.TransparencyKey = Color.Magenta;

Other ways are:

  • Creating a background image, painting the transparent area of it with a specific color and setting it as the form BackgroundImage... then setting the TransparencyKey to that color.
  • Overriding OnPaintBackground method with an empty method.

[EDIT] As Mario states, normally the default transparent color for the key is Magenta.

Upvotes: 20

Related Questions