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.
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:
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://msdn.microsoft.com/en-us/library/wk5b13s4(v=vs.71).aspx