59

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?

Jeffrey Meyer
  • 5,410
  • 7
  • 30
  • 27

6 Answers6

66

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.

Alan
  • 6,501
  • 1
  • 28
  • 24
  • Cool. Never heard of the TextRenderer before. – MusiGenesis Nov 04 '08 at 23:02
  • 2
    For reference: [TextRenderer.MeasureText on MSDN](http://msdn.microsoft.com/en-us/library/system.windows.forms.textrenderer.measuretext.aspx) – Yann Trevin Feb 08 '12 at 07:24
  • 2
    See this thread: http://stackoverflow.com/questions/6704923/textrenderer-measuretext-and-graphics-measurestring-mismatch-in-size and this blog post: http://blogs.msdn.com/b/jfoscoding/archive/2005/10/13/480632.aspx for more information. It would seem that use of TextRenderer.MeasureText is the method applicable to most usage of .Net WinForms. – RenniePet Jun 24 '12 at 02:28
62

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...

Jon Skeet
  • 1,421,763
  • 867
  • 9,128
  • 9,194
  • 19
    In WPF you would use FormattedText, http://msdn.microsoft.com/en-us/library/system.windows.media.formattedtext.aspx. – Todd White Nov 04 '08 at 22:39
  • How would you do that in ASP.NET? – Shimmy Weitzhandler Dec 24 '12 at 22:58
  • 3
    @Shimmy: Well in ASP.NET it will entirely depend on what the browser does. It's really not the sort of thing you'd try to do in a web app - you'd use CSS to control the formatting instead. – Jon Skeet Dec 24 '12 at 22:58
  • @JonSkeet It doesn't have to be accurate, I just need to create a function that makes up a summary of a long file name so truncating it and leaving the extension i.e. msdfjkasdfjhkjhadf...pdf – Shimmy Weitzhandler Dec 24 '12 at 22:59
  • 2
    @Shimmy: It's not a matter of accuracy - it's a matter of it being basically impossible to predict from the server side. You'd need to do it on the client side if anywhere. But anyway, it's definitely not a .NET question. I suggest you ask a new question, giving your requirements very precisely. – Jon Skeet Dec 24 '12 at 23:00
  • @JonSkeet I'll do this in JS. Thanks. – Shimmy Weitzhandler Dec 24 '12 at 23:01
  • @JonSkeet Having problem with Graphics.MeasureString. Using monospaced font Courier New and still i get different values for different characters. Please see my question http://stackoverflow.com/questions/42362999/strange-behavior-while-computing-string-width-in-pixels-for-simulation-of-word-w – Gondil Feb 21 '17 at 10:57
24

In WPF you would use FormattedText.

Todd White
  • 7,870
  • 2
  • 35
  • 34
9

Graphics.MeasureString but its a bit crappy, as is explained and improved upon; here

Andrew Bullock
  • 36,616
  • 34
  • 155
  • 231
4

You would use Graphics.MeasureString.

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

jons911
  • 1,836
  • 15
  • 19
  • WinForms controls expose a `CreateGraphics` method that can be used to create the `Graphics` object in the first place (its constructor is private) – nateirvin Apr 09 '13 at 05:21
1

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

The resulting object will provide the following:

properties available

Other overloads of MeasureString also available.

Thulani Chivandikwa
  • 3,402
  • 30
  • 33