6

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).

Community
  • 1
  • 1
user1207217
  • 547
  • 1
  • 4
  • 15
  • Your best bet is to take @Zarathos's solution listed below. Once you have a transparent `Form` background, you can use images such as _32bpp_ *PNG* `Bitmap`s with _alpha channels_. If you want more flexibility, I'd suggest you look into _WPF_, it can do a lot more of what you are trying to do with varying transparency. – Erik Jan 14 '13 at 00:30
  • A PNG is too inflexible for what I want really .. Perhaps you'd like to make an answer showing where to start with WPF for this sort of thing? Cheers – user1207217 Jan 14 '13 at 22:10
  • See the solution proposed by @AbZy in this post: http://stackoverflow.com/questions/4463363/how-can-i-set-the-opacity-or-transparency-of-a-panel-in-winforms/4464161#4464161 – AntonioJunior Feb 13 '14 at 18:26

4 Answers4

20

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.

Tommaso Belluzzo
  • 23,232
  • 8
  • 74
  • 98
  • 3
    `Color.Magenta` is usually considered to be the default transparency key color. This might make things reusable more easily depending on what you're working on. Also keep in mind that color keying only supports 1 bit alpha (i.e. transparent/not transparent), but no real 8 bit alpha (or more), which is what the OP is looking for. – Mario Jan 14 '13 at 00:19
  • 1
    Overriding OnPaintBackground gave me a black background ... And as Mario stated, this doesn't allow me to blend - It's what I had before I started trying to blend – user1207217 Jan 14 '13 at 22:06
  • Sticking only on the TransparencyKey solution... have you tried calling "SetStyle(ControlStyles.SupportsTransparentBackColor, true);" in the form constructor before? Have you tried using instead "SetStyle(ControlStyles.SupportsTransparentBackColor, true);" and then "this.BackColor = Color.Transparent;"? – Tommaso Belluzzo Jan 14 '13 at 23:10
  • The reason this answer hasn't been accepted yes is beyond me, +1 – Jordan LaPrise Dec 03 '14 at 06:24
3

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.

ahmad mirza
  • 35
  • 1
  • 8
0

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

this.TransparencyKey = Color.Red;
a d
  • 552
  • 2
  • 7
  • 17
-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);
        }
    }
}
noufalcep
  • 3,446
  • 15
  • 33
  • 51