Reputation: 63
I am sure that my question or problem is really strange but this is it:
I am writing a program in c# which has a right to left layout (Arabic language). I am then adding the pictureBox
control to be a background image in the form. I have set the dock of the pictureBox
to be fill so it will be all the form.
Since the picture has some design that involves more than one color, I want to make the label transparent so that it does not have any color and it will take the color from the pictureBox
itself. Here is how I make the label transparent:
label_customer_name.Parent = pictureBox1;
label_customer_name.BackColor = Color.Transparent;
This method works fine to change the color to transparent but there is a problem. When I set the parent of the label to be the pictureBox
, the location of it changes and gets reflected on the other side. For example, if the location is set to be X after the execution, the location will be the form width - x. Any idea how to get rid of this problem?
What I am looking for in brief is:
Is there any other way to make the label transparent without setting the parent to the container control? Or
How to change the layout of PictureBox
and force it to be from left to right.
Just as hint and the funny thing is that: if I add the pictureBox
to a tab control and set the tab control to be right to left, the problem disappears as it seems that the pictureBox
will get the layout from the tab control but it does not get it from the form itself.
Upvotes: 7
Views: 1806
Reputation: 557
Using GDI+ for Drawing Images with Transparency.
see this .
http://www.codeproject.com/Articles/25048/How-to-Use-Transparent-Images-and-Labels-in-Window
Good luck
Upvotes: 2
Reputation: 4489
The Label control become a child of the Form when using the Form Designer. It's the cause of opaque. We need to set Parent property to the PictureBox object after InitializeComponent.
InitializeComponent();
label1.Parent = pictureBox1;
label1.BackColor = Color.Transparent;
Upvotes: 0
Reputation: 26
You can try something like this:
Image im = Image.FromFile("imagePath");
this.BackgroundImage = im;
this.BackgroundImageLayout = ImageLayout.Stretch;
this.DoubleBuffered = true;
Upvotes: 0
Reputation: 1636
if your location changes after assigning the parent, you can keep the current location of label say (x,y) and assign a parent to it and then again set its location to (x,y)
ALSO You can try adding your label control into the picture box to set parent in different way.
pictureBox1.Controls.Add(label_customer_name);
One more solution for making your label control transparent is to set the label control's background similar to your picture box background. You can assign color that is in the background. (This is a dirty solution and i personally will not suggest this.)
Upvotes: 0
Reputation: 847
Use a FlowLayoutPanel
control, that will serve for the hidden requirement, You can set FlowDirection = RightToLeft
and RightToLeft = True
.
Put the BackgroundImage of the panel to your image and add the labels to that panel.
Give it a try it should work.
Upvotes: 0