Calculate string display width in C #?

The Java version of this question has been 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?

+48
c #
Nov 04 '08 at 21:56
source share
6 answers

You have the same problem on this issue as on the Java question - not enough information! It will be different between WinForms and WPF.

For WinForms: Graphics.MeasureString

For WPF, I'm not sure, but I suspect that it will depend on how you draw the text ...

+53
Nov 04 '08 at 10:00
source share

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

Although limited to integer sizes, this one (in tandem with TextRenderer.DrawText) displays a more accurate and significantly better ClearType text than the Graphics.MeasureString / DrawString duo.

+60
Nov 04 '08 at 22:26
source share

In WPF you should use FormattedText .

+21
Nov 04 '08 at 22:37
source share

Graphics.MeasureString , but it's a little flabby, as explained and improved; here

+9
Nov 04 '08 at 10:00
source share

You would use Graphics.MeasureString.

http://msdn.microsoft.com/en-us/library/6xe5hazb.aspx

+4
Nov 04 '08 at 22:01
source share

Graphics.MeasureString ([text to measure], [font used to measure text]);

The resulting object will provide the following:

properties available

Other MeasureString overloads are also available.

+1
Sep 01 '14 at 7:44
source share



All Articles