76

In my C# Form I have a Label that displays a download percentage in the download event:

  this.lblprg.Text = overallpercent.ToString("#0") + "%";

The Label control's BackColor property is set to be transparent and I want it to be displayed over a PictureBox. But that doesn't appear to work correctly, I see a gray background, it doesn't look transparent on top of the picture box. How can I fix this?

8 Answers 8

179

The Label control supports transparency well. It is just that the designer won't let you place the label correctly. The PictureBox control is not a container control so the Form becomes the parent of the label. So you see the form's background.

It is easy to fix by adding a bit of code to the form constructor. You'll need to change the label's Parent property and recalculate it's Location since it is now relative to the picture box instead of the form. Like this:

    public Form1() {
        InitializeComponent();
        var pos = label1.Parent.PointToScreen(label1.Location);
        pos = pictureBox1.PointToClient(pos);
        label1.Parent = pictureBox1;
        label1.Location = pos;
        label1.BackColor = Color.Transparent;
    }

Looks like this at runtime:

enter image description here


Another approach is to solve the design-time problem. That just takes an attribute. Add a reference to System.Design and add a class to your project, paste this code:

using System.ComponentModel;
using System.Windows.Forms;
using System.Windows.Forms.Design;    // Add reference to System.Design

[Designer(typeof(ParentControlDesigner))]
class PictureContainer : PictureBox {}
6
  • Thanks, it works perfectly but the picturebox is a custom ProgressBar and the download details don't appear until the ProgressBar increase at the label location, how can I made it visible always and not just when the ProgressBar touches it? Here a !picture
    – Derezzed
    Feb 22, 2012 at 3:28
  • 1
    I did this, until I realized I did not need a picturebox. The Label control itself has a Image property which might be enough if simple scenarios that does not requires image formating.
    – Larry
    Sep 13, 2012 at 8:34
  • 10
    Also this doesn't work for ProgresBar underneath the label.
    – Bitterblue
    May 23, 2014 at 8:23
  • Maybe this trick could be done with a ProgressBar after all? how to? Mar 19, 2017 at 16:53
  • 3
    Especially your '...and recalculate it's Location since it is now relative to the picture box instead of the form...' is really helpful!
    – PeterCo
    Jan 10, 2019 at 13:58
44

You can just use

label1.Parent = pictureBox1;
label1.BackColor = Color.Transparent; // You can also set this in the designer, as stated by ElDoRado1239
4
  • 3
    Thats Perfect!, never used parent property of label. Well done. :)
    – Bravo
    Jul 27, 2014 at 18:52
  • For me, "Color.Transparent" appears as unknown and "System.Drawing.Color.Transparent" fails after running...
    – kokbira
    Jun 3, 2015 at 18:03
  • Thanks for confirming my feeling the accepted answer is unnecessarily long. Also, you can set the transparent BackColor in the designer, no need to write that out either. Aug 9, 2015 at 3:51
  • Thanks. Setting Parent works fine. Adding the Label to the PictureBox Controls also seems to set the Parent property (ex pictureBox1.Controls.Add(label1);). And the label FlatStyle must be set to "Standard" to allow the label BackColor to be set to Color.Transparent.
    – LT Dan
    Mar 2, 2018 at 14:44
10

You can draw text using TextRenderer which will draw it without background:

private void pictureBox1_Paint(object sender, PaintEventArgs e)
{
    TextRenderer.DrawText(e.Graphics, 
                          overallpercent.ToString("#0") + "%", 
                          this.Font, 
                          new Point(10, 10), 
                          Color.Red);
}

When overallpercent value changes, refresh pictureBox:

pictureBox1.Refresh();

You can also use Graphics.DrawString but TextRenderer.DrawText (using GDI) is faster than DrawString (GDI+)

Also look at another answer here and DrawText reference here

1
  • 2
    Thanks! This was the only solution that helped to put a text (transparent background) on a ProgressBar.
    – Prokurors
    Jul 24, 2015 at 12:37
5

For easy for your design. You can place your label inside a panel. and set background image of panel is what every image you want. set label background is transparent

1
  • 1
    This solution will not work if the image is an animated gif. The animation will not work as the background of a panel.
    – Chun Lin
    May 7, 2016 at 14:08
1

After trying most of the provided solutions without success, the following worked for me:

label1.FlatStyle = FlatStyle.Standard
label1.Parent = pictureBox1

label1.BackColor = Color.Transparent    
1

You most likely not putting the code in the load function. the objects aren't drawn yet if you put in the form initialize section hence nothing happens.

Once the objects are drawn then the load function runs and that will make the form transparents.

   private void ScreenSaverForm_Load(object sender, EventArgs e)
    {            
        label2.FlatStyle = FlatStyle.Standard;
        label2.Parent = pictureBox1;
        label2.BackColor = Color.Transparent;

    }
0

One way which works for everything, but you need to handle the position, on resize, on move etc.. is using a transparent form:

        Form form = new Form();
        form.FormBorderStyle = FormBorderStyle.None;
        form.BackColor = Color.Black;
        form.TransparencyKey = Color.Black;
        form.Owner = this;
        form.Controls.Add(new Label() { Text = "Hello", Left = 0, Top = 0, Font = new Font(FontFamily.GenericSerif, 20), ForeColor = Color.White });
        form.Show();
-3

Using Visual Studio with Windows Form you may apply transparency to labels or other elements by adding using System.Drawing; into Form1.Designer.cs This way you will have Transparency available from the Properties panel ( in Appearance at BackColor ). Or just edit code in Designer.cs this.label1.BackColor = System.Drawing.Color.Transparent;

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.