如何设置单词的背景颜色?

Roy*_*ron 0 c# forms picturebox background-color

我在表格中有一个pictureBox.在那个pictureBox的中间,我已经放置了所选照片的​​名称.现在,我想为所选名称的背景着色.

我怎样才能做到这一点?

Mir*_*Mir 9

我不确定你的意思,但PictureBox的内容是一个图像.如果您只想显示文本,请使用标签.如果希望它具有特定的背景颜色,请将其BackColor属性设置为所需的颜色.

例:

private void Form1_Load(object sender, EventArgs e)
{
    var label = new Label {BackColor = Color.White};
    Controls.Add(label);
}
Run Code Online (Sandbox Code Playgroud)

编辑:

我允许自己重复使用上面Sampath的部分示例,使其适应用户的评论.

void pictureBox1_Paint(object sender, PaintEventArgs e)
{
    using (var font = new Font("Arial", 14))
    {
        const string pictureName = "Picture.jpg";
        var textPosition = new Point(10, 10);
        //Drawing logic begins here.
        var size = e.Graphics.MeasureString(pictureName, font);
        var rect = new RectangleF(textPosition.X, textPosition.Y, size.Width, size.Height);
        //Filling a rectangle before drawing the string.
        e.Graphics.FillRectangle(Brushes.Red, rect);
        e.Graphics.DrawString(pictureName, font, Brushes.Green, textPosition);
    }
}
Run Code Online (Sandbox Code Playgroud)