Transparent Label over Background Image?

  • Thread starter Thread starter Thomas Bandt
  • Start date Start date
T

Thomas Bandt

Hi all,

I've set a background image for my form in OnPaintBackground().
In the layer above I have a set of buttons and labels. Now the
label controls inherits the background color of the parent
control, which is the form ... so I have a ugly white background
under my labels text.

I tried to set the background color of the form to Color.Transparent,
I also placed a panel control in the form - but nothing worked.

Does anyone has an idea to get this working?
 
You could draw the text yourself instead of using labels. I'm not sure
how to do this in mobile but on the desktop I have set the color to a
unused color (like purple) and then filter out that color to make it
transparent.
 
Brian said:
You could draw the text yourself instead of using labels. I'm not sure
how to do this in mobile but on the desktop I have set the color to a
unused color (like purple) and then filter out that color to make it
transparent.

Thank you guys. And you're sure there is no more comfortable way to go?
 
you can create your custom control that will draw its parent background color
onto itself and then draw the text over. If the parent happens to have an
image background, get that image and draw the portion of the image that the
custom label control is on.

Here is the sample code:

public class CustomLabel : Control {
protected override void OnPaint(PaintEventArgs e)
{
e.Graphics.DrawImage(parentBackgroundImage, new Rectangle(0, 0,
this.Width, this.Height), new Rectangle(this.Left, this.Top, this.Width,
this.Height), GraphicsUnit.Pixel);
e.Graphics.DrawString(text, font, brush, new RectangleF(0, 0,
this.Width, this.Height)
}
}
 
Back
Top