Reasons for Why a Winforms Label Does Not Want to Be Transparent

Reasons for why a WinForms label does not want to be transparent?

WinForms doesn't really support transparent controls, but you can make a transparent control yourself. See my answer here.

In your case you should probably subclass the progress bar and override the OnPaint method to draw a text on the progress bar.

Label backcolor not going transparent

The label does not support transparency, you must create your own unique custom control, you can see these code examples.

http://www.codeproject.com/KB/dotnet/transparent_controls_net.aspx
http://www.codeproject.com/KB/vb/uLabelX.aspx

Transparent backcolor of label

If you want anti-aliasing, you won't be able to get it with TransparencyKey, pretty much by definition. TransparencyKey picks a single color, and any pixels of that color become totally transparent. Anti-aliasing uses various shades to simulate smoothed edges; those shades don't match the single color you're making transparent, so those pixels will be opaque, which is exactly what we see in your screenshot.

You need to use what Windows refers to as a "layered window". There are two kinds of layered windows; there's the kind that uses TransparencyKey (which WinForms supports, but won't suit your needs), and the kind that lets you specify a transparency value for each individual pixel of your window (which it looks like WinForms does not support out of the box).

My recommendation would be to use WPF instead of WinForms. WinForms is ancient technology, and really isn't suited to the kind of UI effects you're trying to create here. It doesn't even have good support for semitransparent controls within a form, much less per-pixel alpha for the form itself based on its contents.

WPF can do per-pixel transparency out of the box. Set your window's WindowStyle to None and AllowsTransparency to true, and then you can use all the transparency effects you want. Set your window's background color to Transparent (if you want clicks on empty areas to still go to your window) or {x:None} (if you want clicks on empty areas to go to the window underneath yours) or a semi-transparent brush; layer semitransparent controls or ARGB bitmaps on top of each other; it'll all look great.

If you really want to use WinForms for some reason, I'm guessing you'll have a lot of work ahead of you. I'd suggest Googling for "WinForms layered window" and brushing up on your P/Invoke.

How to make the background of a label transparent in c#

In Windows Forms you can't do this directly. You can work with BackgroundImage.

Try this:

void TransparetBackground(Control C)
{
C.Visible = false;

C.Refresh();
Application.DoEvents();

Rectangle screenRectangle = RectangleToScreen(this.ClientRectangle);
int titleHeight = screenRectangle.Top - this.Top;
int Right = screenRectangle.Left - this.Left;

Bitmap bmp = new Bitmap(this.Width, this.Height);
this.DrawToBitmap(bmp, new Rectangle(0, 0, this.Width, this.Height));
Bitmap bmpImage = new Bitmap(bmp);
bmp = bmpImage.Clone(new Rectangle(C.Location.X+Right, C.Location.Y + titleHeight, C.Width, C.Height), bmpImage.PixelFormat);
C.BackgroundImage = bmp;

C.Visible = true;
}

and in Form_Load:

private void Form1_Load(object sender, EventArgs e)
{
TransparetBackground(label2);
}

and you can see this result:

Sample Image

WinForms picturebox not transparent

It is quite easy all you have to do is make the canvas PictureBox of the same size and set its location also the same as your first picture box. Then set canvas PictureBox back colour to transparent. Now set your first PictureBox as the parent of canvas PictureBox.

You can write the below code on the form load event.

pictureBoxCanvas.Size = pictureBox1.Size;
pictureBoxCanvas.Location = pictureBox1.Location;
pictureBoxCanvas.BackColor = Color.Transparent;
pictureBoxCanvas.BringToFront();
pictureBoxCanvas.Parent = this.pictureBox1;

C# - Why is my dynamic label not transparent even if the parent is set?

The problem I see is here:

lbl_Tile.Location = pb_Tile.Location;

The documentation for Location property:

Gets or sets the coordinates of the upper-left corner of the control relative to the upper-left corner of its container.

In your case the pb_Tile is the container of the lbl_Tile, so to achive the desired location you should use something like

lbl_Tile.Location = new Point(0, 0);

Also you should remove this line

gb_gridBox.Controls.Add(lbl_Tile);

because it changes the Parent of the label. parent.Controls.Add(child) and child.Parent = parent do one and the same.

C# Transparency Isn't Affecting Panel

The first link that @Reza Aghaei sent me worked!

Here it is: https://stackoverflow.com/a/32402532/6804700

Transparent control over PictureBox

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:

Sample Image


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 {}


Related Topics



Leave a reply



Submit