Transparent control over PictureBox

C#BackgroundLabel

C# Problem Overview


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?

C# Solutions


Solution 1 - C#

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 = this.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 {}

Solution 2 - C#

You can just use

label1.Parent = pictureBox1;
label1.BackColor = Color.Transparent; // You can also set this in the designer, as stated by ElDoRado1239

Solution 3 - C#

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

Solution 4 - C#

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

Solution 5 - C#

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    

Solution 6 - C#

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();

Solution 7 - C#

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;

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionDerezzedView Question on Stackoverflow
Solution 1 - C#Hans PassantView Answer on Stackoverflow
Solution 2 - C#WouterView Answer on Stackoverflow
Solution 3 - C#MaciejView Answer on Stackoverflow
Solution 4 - C#Grey WolfView Answer on Stackoverflow
Solution 5 - C#FalconView Answer on Stackoverflow
Solution 6 - C#Stefan PintilieView Answer on Stackoverflow
Solution 7 - C#Andrei MagicView Answer on Stackoverflow