Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

like image 881
user1207217 Avatar asked Jan 14 '13 00:01

user1207217


People also ask

What C is used for?

C programming language is a machine-independent programming language that is mainly used to create many types of applications and operating systems such as Windows, and other complicated programs such as the Oracle database, Git, Python interpreter, and games and is considered a programming foundation in the process of ...

What is the full name of C?

In the real sense it has no meaning or full form. It was developed by Dennis Ritchie and Ken Thompson at AT&T bell Lab. First, they used to call it as B language then later they made some improvement into it and renamed it as C and its superscript as C++ which was invented by Dr.

Why do we write C?

It was mainly developed as a system programming language to write an operating system. The main features of the C language include low-level memory access, a simple set of keywords, and a clean style, these features make C language suitable for system programmings like an operating system or compiler development.

Why is C named so?

Quote from wikipedia: "A successor to the programming language B, C was originally developed at Bell Labs by Dennis Ritchie between 1972 and 1973 to construct utilities running on Unix." The creators want that everyone "see" his language. So he named it "C".


2 Answers

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.

like image 118
Tommaso Belluzzo Avatar answered Sep 28 '22 03:09

Tommaso Belluzzo


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.

like image 24
ahmad mirza Avatar answered Sep 28 '22 05:09

ahmad mirza