How to set the background color of a word? [closed]

為{幸葍}努か 提交于 2020-05-14 12:32:42

问题


I have a pictureBox in a Form. In the middle of that pictureBox, I've placed the name of a chosen photo. Now, I want to color the background of that chosen name.

How can I do that?


回答1:


I'm not sure what you mean, but a PictureBox's content is an image. If you want to simply display text, use a Label. If you want it to have a specific background color, set its BackColor property to the color that you want.

Example:

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

EDIT:

I allowed myself to re-use part of Sampath's example above to adapt it to the user's comment.

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



回答2:


You can try below sample.

Just add a picture box to your form, and add an event handler for the Paint event:

You want to use the Paint event in the PictureBox. You get the graphics reference from e.Graphics, and then use the DrawString() that you have in your sample.

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

Hope this will help to you.




回答3:


Your problem might be as simple as changing the background color property for the control, which you change in the properties window.



来源:https://stackoverflow.com/questions/13880386/how-to-set-the-background-color-of-a-word

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!