Calculate the display width of a string in C#?


Question

A Java version of this question was just answered, and, well, I don't know how to do this in .net.

So how do you calculate the display width of a string in C# / .net?

1
50
11/4/2008 9:56:14 PM

Accepted Answer

You've got the same problem in this question as was present in the Java question - not enough information! It will differ between WinForms and WPF.

For WinForms: Graphics.MeasureString

For WPF I'm not sure, but I suspect it will depend on the exact way you're drawing the text...

56
11/4/2008 10:06:42 PM

An alternative for Windows Forms is the static TextRenderer.MeasureText method.

Although restricted to integer sizes, this (in tandem with TextRenderer.DrawText) renders more accurate and much higher quality ClearType text than the Graphics.MeasureString/DrawString duo.


Licensed under: CC-BY-SA with attribution
Not affiliated with: Stack Overflow
Icon