Rick Strahl's Weblog  

Wind, waves, code and everything in between...
.NET • C# • Markdown • WPF • All Things Web
Contact   •   Articles   •   Products   •   Support   •   Advertise
Sponsored by:
Markdown Monster - The Markdown Editor for Windows

Transparent Labels in WinForms


:P
On this page:

One really annoying thing about WinForms is that there's no built in support for transparency for controls. If you want to create a label and want it to blend into any custom background colors this isn't inherently supported. Although there's a static Color.Transparent color property, when you apply to a label on a form it doesn't actually do anything - you get the same default background color (inherited fro the parent) applied instead.

If you're using the text on top of something that is a solid and named color you can usually make this work by looking at the container' BackColor property in code and just inheriting that:

this.lblVersion.BackColor = this.lblVersion.Parent.BackColor;

in code to inherit the background color dynamically.

However that won't work if your background is an image or some sort of gradient or the content overlays more than one single color or control.

For images there's a workaround though: You can use a PictureBox or Image control and make the label a child of that control:

this.lblVersion.Parent = this.SplashImage;
this.lblVersion.BackColor = Color.Transparent;

You can do this in the constructor after InitializeComponent or even in the Form's Load. Note however that using this approach will cause the label's coordinates now to become relative to the Image or PictureBox control, so you effectively lose the ability to lay out the label in the designer in the correctly location - the label will be stuck as form relative rather than a picture box relevant component.

If all else fails you can also create use raw GDI+ code to draw the label onto the form at the specified position form location. As it turns out it's not terribly difficult to do this: Here's a TransparentLabel class that does this. Didn't test this extensively (and the author warns of some possible rendering inconsistencies) but I plugged it into several of my simple scenarios of overlaying a label on top of various graphics this worked just fine.

Seems kind of odd that Microsoft didn't support transparency for controls natively in WinForms especially given that Color.Transparent is actually allowed. I haven't looked at how WinForms renders but it probably uses GDI+ at some point so rendering transparent backgrounds shouldn't exactly be rocket science. I suspect it has something to do related with font smoothing in Windows but who knows <g>.

Anyway - this is nothing new, but it's something I keep running into with the little bit of WinForms development that I do. Hopefully it'll be helpful for a few of you as well.

Posted in WinForms  

The Voices of Reason


 

wOOdy
February 07, 2008

# re: Transparent Labels in WinForms

Bummer. But isn't it ironic (and frustrating) to know that good ol' FoxPro always had that Backstyle = Transparent and Style = Themed settings? One would think that someting modern as NET should be able to get those simple things done. If FoxPro was able to do it (even without any GDI+), why not NET?

Andre
February 07, 2008

# re: Transparent Labels in WinForms

Transparencies look nice, but they're terrible when it comes to performance. We have a form with lots of input controls and everything feels slow as molasses. We'll probably be switching to a less flashy form with the usual gray background :P

yaip
February 07, 2008

# re: Transparent Labels in WinForms

What about ASP.NET?

Rick Strahl
February 07, 2008

# re: Transparent Labels in WinForms

@yaip - In HTML (and WPF for that matter) all text is transparent by default unless you explicitly set background colors.

@Andre - Interesting. I would think that regardless of how things are laid out it's still GDI+ functions that draw the text. Label might be optimized though in some ways that are slower than the class I mentioned. After all there's probably a reason that stock Label doesn't support transparency. What are you using for transparent labels?

But in general I think it's a good idea to create simple form backgrounds anyway, at least for the core areas where data entry and important data display occurs. Anything else - while it may look nice on first blush - usually is a distraction.

yaip
February 07, 2008

# re: Transparent Labels in WinForms

of course. duh:)

BTW, Telerik announced this: http://www.telerik.com/demos/aspnet/formdecorator/Default.aspx

Very cool.

Doogal
February 08, 2008

# re: Transparent Labels in WinForms

Hey, thanks for the link! I've been reading your blog for years and it was a pleasant surprise to see my site mentioned on here. Let me know if you do have any issues with the code, it certainly isn't perfect.

Bernard
February 11, 2008

# re: Transparent Labels in WinForms

Worse this extends to the image control with no provision for the background being transparent. So you have to use a transparent GIF or PNG - no JPG or BMP.

Rick Strahl
February 11, 2008

# re: Transparent Labels in WinForms

@Bernard - doesn't that make sense though? How would the control know what the background transparency is without the alpha support in the image format? Seems reasonable to me to use an image format that actually supports transparency and that does after all work.

Bernard
February 12, 2008

# re: Transparent Labels in WinForms

What you say is true, except that that leaves either gif which is 8 bit colour only or PNG which GDI+ is notorious for (mis)handling, causing much flicker. Not much left over then, is there?

Why could they not provide a property like VFP does where you can specify that the background be transparent and in addition, also specify which colour it is. Then one could even use MS's own BMP format. After all this is supposed to be new when compared to VFP.

I am getting good at sums :) (validation)

chall3ng3r
March 13, 2008

# re: Transparent Labels in WinForms

Hi,

Thanks for the info. Saved me some time :)

For, me just setting the label's parent property work after initialize component call. I have already set the transparent property of label to Transparent via designer, so no need to change it again.

"this.lblVersion.Parent = this.SplashImage;"

The designer work as well, as i haven't changed anything with the designer code. So, I'm able to use designer, and after all the work, i jut put a line in Form.Load method to make it transparent :)

ciao,

// chall3ng3r //

samie
August 25, 2008

# re: Transparent Labels in WinForms


Tino Mclare
June 10, 2010

# re: Transparent Labels in WinForms

Hi

Im not sure if this is going to cure the problem, but there is a Tranparrent attribute for labels under the 'Web' Tab of the BackColor property.

It works for me, hope this is what your looking for :-)

Tino

John D. Sanders
September 26, 2010

# re: Transparent Labels in WinForms

Dude this is awesome. I have been looking for this for quite awhile. One thing I was wondering if you have solved is how to know if you need to change the text color. That is my next challenge.

So if they pick a dark colored Image I should turn the text white. Ideas?

Anyway this was a great help.

Thanks
JD

aaron
June 21, 2012

# re: Transparent Labels in WinForms

This technique works really well! I have two labels (no text), each with an image from an imagelist. in the ctor of the form (after initializecomponent), I set the parent of the label that is on top of the other label, and the transparency works perfectly.

There are others that suggest a LOT of work with custom controls handling painting, etc. This is a simple and perfect solution

West Wind  © Rick Strahl, West Wind Technologies, 2005 - 2024