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

Reza Aghaei
  • 120,393
  • 18
  • 203
  • 398
arn48
  • 310
  • 4
  • 12
  • http://stackoverflow.com/questions/9358500/making-a-control-transparent – Martin Heralecký Mar 19 '16 at 08:30
  • 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! – TaW Mar 19 '16 at 11:12
  • 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. – Hans Passant Mar 19 '16 at 12:50

1 Answers1

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);
    }
}  
Reza Aghaei
  • 120,393
  • 18
  • 203
  • 398
  • ya i need this as the picture but is there any inbuit methord to do this? – arn48 Mar 19 '16 at 18:12
  • 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. – Reza Aghaei Mar 19 '16 at 19:20