Tuesday, 2 September 2008

Label on top of a PictureBox with transparent backcolor

It´s quite usual to need a label to be drawn in top of a PictureBox with a transparent backcolor.

In normal circumstances, setting the BackColor of the Label to "Transparent" would suffice, but when the label is on top of a PictureBox it doesn´t work as expected, as it draws parent´s backcolor as background, not the picture.

To make the label be drawn properly, it must be in the list of Controls of the PictureBox, what is not achieved by normal drag&dropping the the design view.

So, you would need to make the change manually, for example, in the constructor of the form:

public CTOR()
{
InitializeComponent();
...
this.Controls.Remove(label);
this.pictureBox.Controls.Add(label);
}

Et Voilá!

No comments: