54

I wanted to make my windows form transparent so removed the borders, controls and everything leaving only the forms box, then I tried to the BackColor and TransparencyKey to transparent but it didnt work out as BackColor would not accept transparent color. After searching around I found this at msdn:

SetStyle(ControlStyles.UserPaint, true);
SetStyle(ControlStyles.OptimizedDoubleBuffer, true);
SetStyle(ControlStyles.SupportsTransparentBackColor, true);
this.BackColor = Color.Transparent;
this.TransparencyKey = BackColor;

Unhappyly it did not work either. I still get the grey or any other selected color background.

All I wanted to do is to have the windows form transparent so I could use a background image that would act as if it was my windows form.

I searched around here and saw many topics in regards opacity which is not what I am looking for and also saw some in regards this method I was trying but have not found an answer yet.

Hope anyone can light my path.

UPDATE:

image removed as problem is solved

ahajib
  • 12,838
  • 29
  • 79
  • 120
Prix
  • 19,417
  • 15
  • 73
  • 132
  • Can you please clarify a little what you want to do? I didn't understand what you are trying to achieve with the background image. – Ran Dec 08 '10 at 13:04
  • I want to make my own custom application window in a simple way so instead of using the winforms default window, I am trying to make it transparent to use my background image instead. – Prix Dec 08 '10 at 13:06
  • Do you mean that the background image will be drawn on some other window that is placed behind your window? I don't understand why you can't just override OnPaint and draw the background image there... – Ran Dec 08 '10 at 13:19
  • @Ran I have updated with an example, the green part is remaints of the windows form which i want to make completely transparent. – Prix Dec 08 '10 at 13:26
  • I see. So 2 more questions...:) Did you set the image file as the Form's BackgroundImage property? What is the image's format? – Ran Dec 08 '10 at 13:41
  • It is a 32b png with transparency, using it into a Panel background image it works fine but using it as the background image of the form it looks like the image above. – Prix Dec 09 '10 at 01:36
  • For futur reader. As nobody said that "Transparent" is not a Transparent Key, I'll add it here: It is necessary to choose an existing color for the background, between Color.FromArgb(0, 0, 0) to Color.FromArgb(255, 255, 255), before using this color as a transparency key. – Léon Pelletier May 05 '12 at 01:48

8 Answers8

91

The manner I have used before is to use a wild color (a color no one in their right mind would use) for the BackColor and then set the transparency key to that.

this.BackColor = Color.LimeGreen;
this.TransparencyKey = Color.LimeGreen;
Joel Etherton
  • 37,325
  • 10
  • 89
  • 104
  • 2
    That does not work, when I do that I simple get a Lime background. I have seem this answer in other topics and tried but thanks. – Prix Dec 08 '10 at 13:07
  • @Prix - the only way I've seen this done to use an image as a transparent background for a form is to use `Graphics.Clip` http://msdn.microsoft.com/en-us/library/system.drawing.graphics.clip.aspx. This gets very complex. – Joel Etherton Dec 08 '10 at 13:11
  • 1
    it is not a image as transparent background, it is a image ON the transparent windows form. the only problem i have is the background of the windows form won't go transparent the image is 100% as it should be. – Prix Dec 08 '10 at 13:13
  • @Prix - the solution I posted provides a transparent background for a Windows form in Framework 4.0. (I just tested it as well). However, if you inject an image into the background that needs transparency, you'll have to use a transparent bitmap to accomplish it. If you use the solution above, you could put your image into a label or panel control and just make sure it is "sent to the back". Your image will still need to be a transparent bitmap though. Just throwing a png in there won't do it. – Joel Etherton Dec 08 '10 at 13:16
  • It is a png with transparency and if i set the background color to anything i can perfectly see thru the transparent parts of the image i just can't set the forms transparent. I will try that again with 4.0, was using 3.5 not sure if it will make any difference. – Prix Dec 08 '10 at 13:19
  • @Joel Etherton i have updated the question with an example of how it looks like when i set what you said using 4.0, and thank you for taking the time trying to help me. – Prix Dec 08 '10 at 13:25
  • @Prix - I just tried the solution I gave with the properties on the form set to BackColor: LimeGreen, TransparencyKey: LimeGreen, BackgroundImage: Circle (added resource in my project). Here are the results. Same result with the form border removed. The only catch I see is that the alpha on the red/green don't mix well. The color could be adjusted to be less visible. http://www.joeletherton.com/images/transparent_form.jpg – Joel Etherton Dec 08 '10 at 13:33
  • Joel could you perhaps send me this project so I can see what I could possible be doing wrong ? I make a clean project just to try again what you said but still not working ;( – Prix Dec 08 '10 at 13:36
  • @Prix - I uploaded it to this address: http://www.joeletherton.com/zips/WindowsFormsApplication2.zip – Joel Etherton Dec 08 '10 at 13:38
  • @Prix - also, I didn't use any code behind to accomplish it. Just forms properties settings and a resource file. – Joel Etherton Dec 08 '10 at 13:39
  • @Joel Etherton thanks for taking all this trouble, are you using windows 7 or vista ? I am on windows xp and it simple does not work ... even yours give me a gren box with the 2 circles on it. – Prix Dec 08 '10 at 13:48
  • OK found the temporary solution, using a Panel with the LimeGreen background does the trick but without, just setting backcolor on the main form I am not able to acomplish it. Thanks a lot for your patiency. – Prix Dec 08 '10 at 13:53
  • @Prix - I'm on Server 2008. That's a surprising development. I would have thought that the framework would be have the same regardless of operating system. – Joel Etherton Dec 08 '10 at 13:54
  • Yeah I dont know what is different to cause this but the panel goes transparent just fine but if u set the background image it won't go transparent. I really appreciated your help ! – Prix Dec 08 '10 at 13:59
  • using lime green background all the time – Arnis Lapsa Dec 08 '10 at 14:07
  • The best solution I found after couple of hours search – Shaahin Jan 03 '15 at 09:08
  • semi-transparent : this.Opacity = 0.3; – camino Aug 13 '18 at 13:04
32

A simple solution to get a transparent background in a windows form is to overwrite the OnPaintBackground method like this:

protected override void OnPaintBackground(PaintEventArgs e)
{
    //empty implementation
}

(Notice that the base.OnpaintBackground(e) is removed from the function)

Alexander Cosman
  • 1,117
  • 11
  • 20
  • I noticed that the accepted answer flashed the default background color sometimes before the transparency kicked in. This seems to work reliably for my purposes. Thanks! – Fls'Zen Feb 01 '13 at 15:26
  • 4
    This answer is better because it doesn't cause weird things like this: http://i.imgur.com/lYFunRT.png (notice the weird "lime green" shadow) – derekantrican Aug 16 '17 at 19:11
10

Here was my solution:

In the constructors add these two lines:

this.BackColor = Color.LimeGreen;
this.TransparencyKey = Color.LimeGreen;

In your form, add this method:

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

Be warned, not only is this form fully transparent inside the frame, but you can also click through it. However, it might be cool to draw an image onto it and make the form able to be dragged everywhere to create a custom shaped form.

AustinWBryan
  • 3,249
  • 3
  • 24
  • 42
AyrA
  • 763
  • 10
  • 17
  • 1
    Hi, AyrA! I was wondering... Could I make this not-click-through? I am tryna make a screenshot program... – Momoro Oct 30 '19 at 22:23
9

I've tried the solutions above (and also) many other solutions from other posts.

In my case, I did it with the following setup:

public partial class WaitingDialog : Form
{
    public WaitingDialog()
    {
        InitializeComponent();

        SetStyle(ControlStyles.SupportsTransparentBackColor, true);
        this.BackColor = Color.Transparent;

        // Other stuff
    }

    protected override void OnPaintBackground(PaintEventArgs e) { /* Ignore */ }
}

As you can see, this is a mix of previously given answers.

Joel
  • 7,401
  • 4
  • 52
  • 58
  • 1
    Using this I had problems with the background becoming completely white if I updated the text of a label on the form i.e. it loses it's transparency. – kjbartel Jul 13 '15 at 08:30
6

My solution was extremely close to Joel's (Not Etherton, just plain Joel):

public partial class WaitingDialog : Form
{
    public WaitingDialog()
    {
        InitializeComponent();

        SetStyle(ControlStyles.SupportsTransparentBackColor, true);
        this.BackColor = Color.Transparent;
        this.TransparencyKey = Color.Transparent; // I had to add this to get it to work.

        // Other stuff
    }

    protected override void OnPaintBackground(PaintEventArgs e) { /* Ignore */ }
}
roncli
  • 196
  • 2
  • 8
  • This did not work for me. Other controls which I had on the form became semi-transparent also rather than just the background. – kjbartel Jul 13 '15 at 08:28
2

I had drawn a splash screen (32bpp BGRA) with "transparent" background color in VS2013 and put a pictureBox in a form for display. For me a combination of above answers worked:

public Form1()
{
    InitializeComponent();

    SetStyle(ControlStyles.SupportsTransparentBackColor, true);
    this.BackColor = this.pictureBox1.BackColor;
    this.TransparencyKey = this.pictureBox1.BackColor;
}

So make sure you use the same BackColor everywhere and set that color as the TransparencyKey.

AustinWBryan
  • 3,249
  • 3
  • 24
  • 42
MaLe
  • 21
  • 3
2

I tried almost all of this. but still couldn't work. Finally I found it was because of 24bitmap problems. If you tried some bitmap which less than 24bit. Most of those above methods should work.

Zhi
  • 29
  • 2
2

What works for me is using a specific color instead of the real ability of .png to represent transparency.

So, what you can do is take your background image, and paint the transparent area with a specific color (Magenta always seemed appropriate to me...).

Set the image as the Form's BackgrounImage property, and set the color as the Form's TransparencyKey. No need for changes in the Control's style, and no need for BackColor.

I've tryed it right now and it worked for me...

Ran
  • 5,989
  • 1
  • 24
  • 26