How to make two transparent layer with c#?
Asked Answered
L

1

1

There are three consecutive layers,
picturebox1(.jpg) -> label1 -> picturebox2(.png transparent) what i want is to make the label1 and pictrurebox2 transparent to the picturebox1 so that label1 can be see through the picturebox2 but its not working..

public Form1()
{
    InitializeComponent();
    label1.Parent = pictureBox1;
    label1.BackColor = Color.Transparent;
    pictureBox2.Parent = pictureBox1;
    pictureBox2.BackColor = Color.Transparent;
    picturebox2.BringToFront(); 
}

so please help me

Lennon answered 19/3, 2016 at 7:20 Comment(3)
#9359000Manamanacle
Not sure about the positioning, but you probably need to make the label be a child to pb2, ie all controls must by nested one by one. This means: No overlapping!Bogeyman
You can only see the Parent, stacking effects do not work. Simplest workaround is to use TextRenderer.DrawText() in pictureBox1's Paint event instead of a Label. Also a lot cheaper, but you have to write code. No need to stop there btw, now you can also use Graphics.DrawImage() in that event and you don't need pictureBox2 anymore.Marks
F
3

If you need a control support transparency, you should override painting of the control and draw the control in this order:

  • Draw all controls in the same container which are under your control (based on z-index) on a bitmap.
  • Then draw that bitmap on graphics of your control.
  • At last draw content of your control.
  • Also the BackColor of your control should be Color.Transparent.

Here is the result of creating TransparentLabel and TransparentPictureBox controls. In the below image, there is label, image, label, image and then label in order and as you can see picture boxes and labels has been rendered having transparent background and respecting the z-index:

enter image description here

Transparent Label

class TransparentLabel : Label
{
    public TransparentLabel()
    {
        this.BackColor = Color.Transparent;
    }
    protected override void OnPaint(PaintEventArgs e)
    {
        if (Parent != null && this.BackColor == Color.Transparent)
        {
            using (var bmp = new Bitmap(Parent.Width, Parent.Height))
            {
                Parent.Controls.Cast<Control>()
                      .Where(c => Parent.Controls.GetChildIndex(c) > Parent.Controls.GetChildIndex(this))
                      .Where(c => c.Bounds.IntersectsWith(this.Bounds))
                      .OrderByDescending(c => Parent.Controls.GetChildIndex(c))
                      .ToList()
                      .ForEach(c => c.DrawToBitmap(bmp, c.Bounds));

                e.Graphics.DrawImage(bmp, -Left, -Top);

            }
        }
        base.OnPaint(e);
    }
}

Transparent PictureBox

class TransparentPictureBox : PictureBox
{
    public TransparentPictureBox()
    {
        this.BackColor = Color.Transparent;
    }
    protected override void OnPaint(PaintEventArgs e)
    {
        if (Parent != null && this.BackColor==Color.Transparent)
        {
            using (var bmp = new Bitmap(Parent.Width, Parent.Height))
            {
                Parent.Controls.Cast<Control>()
                      .Where(c => Parent.Controls.GetChildIndex(c) > Parent.Controls.GetChildIndex(this))
                      .Where(c => c.Bounds.IntersectsWith(this.Bounds))
                      .OrderByDescending(c => Parent.Controls.GetChildIndex(c))
                      .ToList()
                      .ForEach(c => c.DrawToBitmap(bmp, c.Bounds));

                e.Graphics.DrawImage(bmp, -Left, -Top);

            }
        }
        base.OnPaint(e);
    }
}  
Fregoso answered 19/3, 2016 at 13:3 Comment(2)
ya i need this as the picture but is there any inbuit methord to do this?Lennon
There is no built-in support for this in windows forms controls. You can customize controls or create new controls like this. As another option if you only need text and images, you can create multiple layers in a control using GDI+ and you don't need to use multiple transparent controls.Fregoso

© 2022 - 2024 — McMap. All rights reserved.