2

I have form as in the image below.

enter image description here

I want to see label1 and label3 through label2. (I just want to see only the border of the label2). I have changed the BackColor in label2 to Transparent. But the result is same like the above picture.

Lakindu
  • 85
  • 2
  • 2
  • 5
  • Assuming Winforms: The sad truth is: __This is not possible.__ (Unless you owner-draw it - and even then only for simple cases like yours..) – TaW Sep 14 '14 at 08:24
  • http://stackoverflow.com/questions/25948454/how-to-set-background-color-to-transparent-for-a-richtextbox-in-c-sharp/25948758#25948758 – Mehdi Khademloo May 20 '17 at 13:09

1 Answers1

11

In Windows Forms you can't do this directly. You can work with BackgroundImage.

Try this:

void TransparetBackground(Control C)
{
    C.Visible = false;

    C.Refresh();
    Application.DoEvents();

    Rectangle screenRectangle = RectangleToScreen(this.ClientRectangle);
    int titleHeight = screenRectangle.Top - this.Top;
    int Right = screenRectangle.Left - this.Left;

    Bitmap bmp = new Bitmap(this.Width, this.Height);
    this.DrawToBitmap(bmp, new Rectangle(0, 0, this.Width, this.Height));
    Bitmap bmpImage = new Bitmap(bmp);
    bmp = bmpImage.Clone(new Rectangle(C.Location.X+Right, C.Location.Y + titleHeight, C.Width, C.Height), bmpImage.PixelFormat);
    C.BackgroundImage = bmp;

    C.Visible = true;
}

and in Form_Load:

private void Form1_Load(object sender, EventArgs e)
{
    TransparetBackground(label2);
}

and you can see this result:

enter image description here

Mohammad Dehghan
  • 17,853
  • 3
  • 55
  • 72
Mehdi Khademloo
  • 2,754
  • 2
  • 20
  • 40
  • I realize this is for .NET Framework, but in .NET Core 3.1 Winforms, this generates an out of memory exception on the .Clone line. – A. Niese Oct 10 '20 at 04:07