如何在图片框C#中添加标签透明度?

我在这里创建一个程序可以添加标签和图片框。

所有控件必须是面板的子项。

我使用这样的代码:

panel2.Controls.Add(picturebox1); panel2.Controls.Add(label1); 

是的,问题是我想要标签超过图片框。

我设置了代码:

 label1.Parent = pictureBox1; label1.BackColor = Color.Transparent; 

更新:

因为控件只在我想通过button_event创建时创建。 比如,创建图片框,创建标签文本。 这不是在我想要使用之前创建的。

我创建此控件的代码:

 public PictureBox ctrl = new PictureBox(); public void btnAddLogo_Click(object sender, EventArgs e) { Random rnd = new Random(); int randNumber = rnd.Next(1, 1000); String picName = "Pic_" + randNumber; ctrl.Location = new Point(200, 170); ctrl.Size = new System.Drawing.Size(100, 60); ctrl.Name = picName; ctrl.BackgroundImageLayout = ImageLayout.Zoom; ctrl.Font = new System.Drawing.Font("NativePrinterFontA", 10F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((byte) (0))); ctrl.BackColor = Color.Chocolate; panel2.Controls.Add(ctrl); } private Label ctrLabel = new Label(); public void btnAddCharacter_Click(object sender, EventArgs e) { Random rnd = new Random(); int randNumber = rnd.Next(1, 1000); String LableName = "Lbl_" + randNumber; ctrLabel.Name = LableName; ctrLabel.AutoSize = true; ctrLabel.Text = txtIDImg.Text; ctrLabel.BackColor = Color.Transparent; panel2.Controls.Add(ctrLabel); } 

但结果表明:

标签未在图片框中。它得到面板是父母。面板1添加了图像

透明度确实适用于嵌套控件; 但winforms下的重叠控件不支持 。 期。

您可以尝试使用两个标签来解决方法, 一个嵌套在 pb 的面板中, 另一个嵌套在 pb中

这是一个例子:

在此处输入图像描述

  Label l1 = new Label() { Text = "Hello World", BackColor = Color.Transparent }; Label l2 = new Label() { Text = "Hello World", BackColor = Color.Transparent }; l1.Parent = scrollPanel; l2.Parent = picBox; Point pt = new Point(picBox.Right - 30, 30); l1.Location = pt; pt.Offset(-picBox.Left, -picBox.Top); l2.Location = pt; 

上面的代码也可以放入一个可重用的函数中:

 Label overlayLabel(Label source, Control target) { Label old = source.Tag as Label; if (old != null && old.Parent == target) target.Controls.Remove(old); Label lbl = new Label(); // copy all necessary properties here: lbl.Text = source.Text; lbl.Font = source.Font; lbl.AutoSize = source.AutoSize; lbl.Size = source.Size; lbl.Anchor = source.Anchor; // may work or not! lbl.BackColor= source.BackColor; lbl.ForeColor = source.ForeColor; // etc.. Point pt = source.Location; pt.Offset(-target.Left , -target.Top); lbl.Location = pt; lbl.Parent = target; source.Tag = lbl; return lbl; } 

在你的代码中,你可以这样称呼它; 您可以存储返回的引用:

  panel2.Controls.Add(ctrLabel); Label ctrLabelOverlay = overlayLabel(ctrLabel, ctrl ); 

..或者丢弃它,因为它负责清理以前的覆盖,它存储在TagLabel ..:

  panel2.Controls.Add(ctrLabel); overlayLabel(ctrLabel, ctrl ); 

最直接的方法是自己绘制那些东西,即文本和图像。 面板的Paint事件中有两行左右..:

 if (img != null) { Rectangle rect = new Rectangle(pt1, img.Size); e.Graphics.DrawImage(img, rect); e.Graphics.DrawString("Hello World", Font, Brushes.Black, pt2); } 

您只需要计算两个位置pt1pt2 。 如果您的Picturbox正在拉伸或缩放,您还需要将源矩形写入可缩放/拉伸图像的DrawImage重载。

强制执行显示调用panel2.Invalidate每当有什么变化..

更简单,function更强大,除非您需要LabelPictureBox特殊function。

请注意代码中发生的所有事情,所以不要指望设计器中出现的东西。 编写代码以便它确实在VS设计器中显示并不是那么容易。

Windows窗体不支持真正的透明度,请仔细阅读。 http://www.broculos.net/2008/03/how-to-use-transparent-images-and.html#.Vq0q2jZuncc