问题
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