c# - 如何在 Picturebox C# 中添加标签透明度?

标签 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);
}

但结果显示如下:

Label not over in picture box. It gets panel is a parent. Panel 1 was added image

最佳答案

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

您可以尝试使用两个标签来解决此问题,一个嵌套在 pb 面板中,另一个嵌套在PB

这是一个例子:

enter image description here

    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 );

..或者丢弃它,因为它负责清理之前的覆盖层,该覆盖层存储在LabelTag中..:

 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 正在拉伸(stretch)或缩放,您还需要将源矩形写入可以缩放/拉伸(stretch)图像的 DrawImage 重载中。

每当发生变化时强制显示调用panel2.Invalidate..

更简单,更强大,除非您需要 LabelPictureBox 的特殊功能..

请注意,所有事情都发生在代码中,因此不要指望事情会显示在设计器中。编写代码以使其确实显示在 VS 设计器中并不那么容易。

关于c# - 如何在 Picturebox C# 中添加标签透明度?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35107257/

相关文章:

c# - ASP.NET Repeater : Is it possible to display on the first item one element, 但不适用于其余项目?

c# - 在此处获取 XElement 的行号

c# - 更改 ASP.NET 标识消息

c# - 基于二维图 block 的 map - 如何在边缘重复 map ?

c# - 提高将数百万张图片存储到数据库中的性能

c# - 根据其中一项和自定义条件对列表进行排序

c# - 类似于控制台的控件,允许完全控制单个文本格式

c# - 为什么我的查询字符串参数被 url 解码两次?

c# - ASP.NET:设置十进制值 Response.StatusCode

c# - WebClient.DownloadString 使用了错误的编码