Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/275.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 如何设置单词的背景色?_C#_Forms_Background Color_Picturebox - Fatal编程技术网

C# 如何设置单词的背景色?

C# 如何设置单词的背景色?,c#,forms,background-color,picturebox,C#,Forms,Background Color,Picturebox,我有一个表格中的图片盒。在那个图片框的中间,我放了一张选择的照片的名字。 现在,我想给所选名字的背景上色 我该怎么做呢?您可以试试下面的示例 只需将一个图片框添加到表单中,然后为绘制事件添加一个事件处理程序: 您想在PictureBox中使用绘制事件。从e.graphics获取图形参考,然后使用样本中的DrawString() private void pictureBox1_Paint(object sender, PaintEventArgs e) { using (Font myF

我有一个表格中的图片盒。在那个图片框的中间,我放了一张选择的照片的名字。 现在,我想给所选名字的背景上色


我该怎么做呢?

您可以试试下面的示例

只需将一个图片框添加到表单中,然后为绘制事件添加一个事件处理程序:

您想在PictureBox中使用绘制事件。从e.graphics获取图形参考,然后使用样本中的DrawString()

private void pictureBox1_Paint(object sender, PaintEventArgs e)
{
    using (Font myFont = new Font("Arial", 14))
    {
        e.Graphics.DrawString("Hello .NET Guide!", myFont, Brushes.Green, new Point(2, 2));
    }
}

希望这能对你有所帮助。

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

例如:

private void Form1_Load(object sender, EventArgs e)
{
    var label = new Label {BackColor = Color.White};
    Controls.Add(label);
}
编辑:

我允许自己重复使用上面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);
    }
}

您的问题可能很简单,只需更改控件的“背景色”属性,您可以在“属性”窗口中更改该属性。

一些代码可以帮助用户。你怎么称呼这个名字的?有标签吗?你试过设置标签的背景吗?如果您使用的是不同的技术,那么代码将非常有用!有完整源代码的最终解决方案吗?嘿,我的意思是我有一张照片的名字。这张照片显示在图片框中。我想改变名字的背景(不是照片的背景)。。。我已经用“e.Graphics.DrawString”把这个词放在图片盒的中心。我能做什么?Sampath,Eve,非常感谢!非常感谢!这将使用特定颜色(即前景色)绘制文本。OP询问如何更改背景色。我能想到的唯一方法是先画一个所需背景色的矩形,然后像你所做的那样画文本。