Graphics in C # (.NET)

I use this code to draw text in a panel:

Graphics g = panel1.CreateGraphics();
g.DrawString(...);

So, I want to know what size the input text will have when rendering in the panel.

+5
source share
2 answers

Use g.MeasureString()to get line width in 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);
+7
source

You can also use TextRenderer.MeasureText , which is sometimes easier to use than MeasureString.

+1
source

All Articles