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?

Nic Szerman
  • 1,834
  • 18
  • 25
Derezzed
  • 1,093
  • 3
  • 11
  • 15

8 Answers8

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 {}
Hans Passant
  • 922,412
  • 146
  • 1,693
  • 2,536
  • 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](http://i.imm.io/h342.jpeg) – Derezzed Feb 22 '12 at 03: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 '12 at 08:34
  • 10
    Also this doesn't work for `ProgresBar` underneath the label. – Bitterblue May 23 '14 at 08:23
  • Maybe this trick could be done with a ProgressBar after all? how to? – ElektroStudios Mar 19 '17 at 16:53
  • For the second approach make sure that the label belongs to the PictureBox (PictureContainer). This can be seen and set in the Document Outline, where the label must be a subnode of the PictureBox. – GDS May 01 '17 at 05:03
  • 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 '19 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
Wouter
  • 575
  • 6
  • 19
  • 3
    Thats Perfect!, never used parent property of label. Well done. :) – Bravo Jul 27 '14 at 18:52
  • For me, "Color.Transparent" appears as unknown and "System.Drawing.Color.Transparent" fails after running... – kokbira Jun 03 '15 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. – ElDoRado1239 Aug 09 '15 at 03: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 02 '18 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

Community
  • 1
  • 1
Maciej
  • 7,871
  • 1
  • 31
  • 36
  • 2
    Thanks! This was the only solution that helped to put a text (transparent background) on a ProgressBar. – Prokurors Jul 24 '15 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

Wolf
  • 6,361
  • 2
  • 28
  • 25
  • 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 07 '16 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    
Falcon
  • 41
  • 3
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;

    }
Harry
  • 11
  • 1
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();
Stefan Pintilie
  • 677
  • 8
  • 5
-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;