Graphics in C# (.NET)
I use this code for drawing text in a panel:
Graphics g = panel1.CreateGraphics();
g.DrawString(...);
So I want to know 开发者_如何转开发what size the input text will be when rendered in the panel.
Use g.MeasureString()
to get the width of a string in the grapic context.
// Set up string.
string measureString = "Measure String";
Font stringFont = new Font("Arial", 16);
// Measure string.
SizeF stringSize = new SizeF();
stringSize = e.Graphics.MeasureString(measureString, stringFont);
You can also use TextRenderer.MeasureText which is sometimes easier to use than MeasureString.
精彩评论