Transparent PictureBox control

In my C # form, I have a label that displays the percentage of load in the load event:

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

The BackColor property of the Label control is set to transparent, and I want it to appear above the PictureBox. But this does not work correctly, I see a gray background, it does not look transparent on top of the image window. How can i fix this?

+64
c # background label
Feb 22 2018-12-12T00:
source share
5 answers

Label control supports transparency. Simply, the designer will not allow you to place the shortcut correctly. The PictureBox control is not a container control, so the form becomes the label's parent. So you see the background of the form.

Easy to fix by adding some code to the form constructor. You will need to change the property of the parent label and recalculate its location, since it is now relative to the image field 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; } 

It looks like runtime:

enter image description here




Another approach is to solve the development time problem. It just takes an attribute. Add a link 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 {} 
+151
Feb 22 2018-12-12T00:
source share

You can just use

 label1.Parent = pictureBox1; label1.BackColor = Color.Transparent; // You can also set this in the designer, as stated by ElDoRado1239 
+38
Apr 12 '13 at 23:18
source share

You can draw text using a TextRenderer, which will draw it without a 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 the total value of the variable changes, update the 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 link here

+9
Feb 22 2018-12-12T00:
source share

Easy for your design. You can place your label inside the panel. and setting the background image of the panel is what you want. set label background transparent

+5
Apr 02 '14 at 4:05
source share

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

-3
Apr 19 '15 at 13:17
source share



All Articles