Transparent backgrounds of Labels and Pictureboxes in C#

In C# 2008 Windows Forms, the background of a Picturebox or Label object cannot be made transparent by default. This causes problems like those displayed in this before/after comparison. 

Comparison screenshots with and without transparency

While it is possible to fix this problem by overriding the OnPaint method with Alpha Blending, or using GDI+ Graphics objects, or drawing directly on the form, I am not satisfied with those solutions. My preferences lean toward highly maintainable code. Where workarounds are necessary, I prefer those workarounds to be as straightforward as possible.

In this case, the picturebox and label objects do not use their own .BackgroundImage; they use the .BackgroundImage of their .Parent object. Unfortunately, they also take their .Location origin from the .Parent object instead of the form. In my case the origin was only 10 pixels off from its initial value. With a minor concession to the locations of my pictureboxes and labels, I worked around the transparency issue by placing this code into the form constructor:

// Make background of pictures and labels over the metal gradient image transparent.
pictCmsSetLED.Parent = pictMetalGradient;
pictManufactureDateLED.Parent = pictMetalGradient;
pictPartNumberLED.Parent = pictMetalGradient;
pictReleaseCertificateLED.Parent = pictMetalGradient;
pictSerialNumberLED.Parent = pictMetalGradient;
lblSerialNumber.Parent = pictMetalGradient;
lblPartNumber.Parent = pictMetalGradient;
lblCmsSet.Parent = pictMetalGradient;
lblReleaseCertificate.Parent = pictMetalGradient;
lblManufactureDate.Parent = pictMetalGradient;
lblManufactureDateMMYYYY.Parent = pictMetalGradient;

 

Inspired by:

http://stackoverflow.com/questions/5522337/c-sharp-picturebox-transparent-background-doesnt-seem-to-work

A similar workaround which also addresses positioning via code:

The fundamental use of peptides for human body should not be overused with products like Thymosin Beta 4 comes in a two milligram bottle and this bulk peptide is being researched with vitro test subjects for it’s healing viagra for sale canada properties. Cannabis is proven to reduce the ailments that many people suffer from today like the intense competition,the fast pace if wolk and life,that has increased the psychological stress and emotional burden of the people.The psychological pressure will lead to psychological imbalance,nervous system dysfunction and endocrine disorders so that caused various of diseases.Keep a healthy state of mind,improve the psychological quality is the most powerful weapon to bring our blood clotting disorder. cost of prescription viagra in stock Australia is no different and levitra online leads in the number of auto accidents injuries here in Naples, FL, as well. Today, when science has given us a wide range of treatments, treating ED levitra uk is just a simple task and need little awareness. http://stackoverflow.com/questions/9387267/transparent-background-label-over-picturebox

public Form1() { 
        InitializeComponent(); 
        var pos = this.PointToScreen(label1.Location); 
        pos = pictureBox1.PointToClient(pos); 
        label1.Parent = pictureBox1; 
        label1.Location = pos; 
        label1.BackColor = Color.Transparent; 
    } 

The road not travelled, but nonetheless interesting:

http://www.daniweb.com/software-development/csharp/threads/280344/transparent-picturebox

http://msdn.microsoft.com/en-us/library/aa452850.aspx

http://social.msdn.microsoft.com/forums/en-US/netfxcompact/thread/9aeab184-2a45-4bd4-8157-2a629523e80b/

http://social.msdn.microsoft.com/forums/en-US/vssmartdevicesvbcs/thread/a3f2cb62-4473-44d3-ad94-04f69ed52758/

http://msdn.microsoft.com/en-us/library/wk5b13s4(v=vs.71).aspx

http://social.msdn.microsoft.com/Forums/en/csharplanguage/thread/071adb8f-3616-46e6-aecd-1b1f46e61df6

 

This entry was posted in Parenting. Bookmark the permalink.

3 Responses to Transparent backgrounds of Labels and Pictureboxes in C#

  1. bhavesh says:

    Good Post. Thanx

  2. Eric says:

    I’m glad you found it useful.

  3. Mariano says:

    Wonderful post, formidable. Thank you

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.