C# 在图片框上添加标签
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/10400943/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Add a Label over Picturebox
提问by Antonio Teh Sumtin
I am trying to write some text over my picturebox so I thought the easiest and best thing to do is draw label over it. This is what I did:
我试图在我的图片框上写一些文字,所以我认为最简单和最好的方法是在它上面画标签。这就是我所做的:
PB = new PictureBox();
PB.Image = Properties.Resources.Image;
PB.BackColor = Color.Transparent;
PB.SizeMode = System.Windows.Forms.PictureBoxSizeMode.StretchImage;
PB.Size = new System.Drawing.Size(120, 30);
PB.Location = new System.Drawing.Point(100, 100);
lblPB.Parent = PB;
lblPB.BackColor = Color.Transparent;
lblPB.Text = "Text";
Controls.AddRange(new System.Windows.Forms.Control[] { this.PB });
I get blank page with no PictureBoxes. What am I doing wrong?
我得到没有图片框的空白页。我究竟做错了什么?
采纳答案by prestomanifesto
While all these answers work, you should consider opting for a cleaner solution. You can instead use the picturebox's Paintevent:
虽然所有这些答案都有效,但您应该考虑选择更清洁的解决方案。您可以改为使用图片框的Paint事件:
PB = new PictureBox();
PB.Paint += new PaintEventHandler((sender, e) =>
{
e.Graphics.TextRenderingHint = System.Drawing.Text.TextRenderingHint.AntiAlias;
e.Graphics.DrawString("Text", Font, Brushes.Black, 0, 0);
});
//... rest of your code
EditTo draw the text centered:
编辑要绘制居中的文本:
PB.Paint += new PaintEventHandler((sender, e) =>
{
e.Graphics.TextRenderingHint = System.Drawing.Text.TextRenderingHint.ClearTypeGridFit;
string text = "Text";
SizeF textSize = e.Graphics.MeasureString(text, Font);
PointF locationToDraw = new PointF();
locationToDraw.X = (PB.Width / 2) - (textSize.Width / 2);
locationToDraw.Y = (PB.Height / 2) - (textSize.Height / 2);
e.Graphics.DrawString(text, Font, Brushes.Black, locationToDraw);
});
回答by Kendall Frey
Instead of
代替
lblPB.Parent = PB;
do
做
PB.Controls.Add(lblPB);
回答by Omar
You have to add the control to the PictureBox. So:
您必须将控件添加到PictureBox. 所以:
PB.Controls.Add(lblPB):
EDIT:
编辑:
I get blank page with no PictureBoxes.
我得到没有图片框的空白页。
You didn't see the picturebox because it has the same backcolor of the Form. So try to set BorderStyle and the BackColor. Another mistake is that probably you haven't set the location of the label. So:
您没有看到图片框,因为它具有与表单相同的背景色。所以尝试设置 BorderStyle 和 BackColor。另一个错误是您可能没有设置标签的位置。所以:
PB.BorderStyle = BorderStyle.FixedSingle;
PB.BackColor = Color.White;
lblPB.Location = new Point(0,0);
回答by Zerato
There's another way to do it. It's very simple but probably not the best one. (I'm a beginner, so I like things simple)
还有另一种方法可以做到。这很简单,但可能不是最好的。(我是初学者,所以我喜欢简单的东西)
If I've understood your question right, you want to put the label above/on top of the pictureBox. The following line of code will do that.
如果我正确理解您的问题,您想将标签放在图片框上方/顶部。以下代码行将执行此操作。
myLabelsName.BringToFront();
Now, your question were already answered, but maybe this can help somebody else.
现在,您的问题已经得到解答,但也许这可以帮助其他人。
回答by Jinwoo.Seo
I tried this. (no use picturebox)
我试过这个。(不使用图片框)
- Use "Panel" control first
- Set panel's BackgroundImage & BackgroundImageLayout (Stretch)
- Add Label Inside panel
- 首先使用“面板”控件
- 设置面板的 BackgroundImage & BackgroundImageLayout (Stretch)
- 添加标签内部面板
that's all
就这样

