13

I need to build a function in a class library that take a string and a specific font for this string then get the width of the string

So how could I get the string boundary width ?

Bala R
  • 107,317
  • 23
  • 199
  • 210
AshOoO
  • 1,108
  • 3
  • 13
  • 34
  • Could be merged with this : http://stackoverflow.com/questions/5553965/programmatically-measure-string-in-asp-net – digEmAll Oct 10 '11 at 14:14
  • Measuring the size of a string for a website sounds questionable. There is no way to tell what size the browser is going to display the font AFAIK. – davisoa Oct 10 '11 at 14:15
  • A specific font is not enough to know this. You also need to know which renderer will display the text. For example Apple and MS render text very differently, and I wouldn't be surprised if that affected the graphical width of a text. If you try to get the width of text displayed in html on the server-side(as your tags seem to imply) that's most likely not possible. – CodesInChaos Oct 10 '11 at 14:16
  • Possible duplicate of [How can I convert a string length to a pixel unit?](http://stackoverflow.com/questions/451903/how-can-i-convert-a-string-length-to-a-pixel-unit) – Jim Fell May 17 '16 at 17:17

3 Answers3

18

Another way to do this is with a TextRenderer, and call its MeasureString method, passing the string and the font type.

MSDN Example:

private void MeasureText1(PaintEventArgs e)
{
    String text1 = "Measure this text";
    Font arialBold = new Font("Arial", 12.0F);
    Size textSize = TextRenderer.MeasureText(text1, arialBold);
    TextRenderer.DrawText(e.Graphics, text1, arialBold, 
        new Rectangle(new Point(10, 10), textSize), Color.Red);  
}

NOTE: This is just an alternate solution to the (equally valid) one already posted by @Neil Barnwell (in case you already have a reference to System.Windows.Forms in your project, this might be more convenient).

Michael Myers
  • 188,989
  • 46
  • 291
  • 292
Josh Darnell
  • 11,304
  • 9
  • 38
  • 66
  • I think this just work within a windows application not class library project ! – AshOoO Oct 18 '11 at 11:41
  • 1
    @AshOoO I agree, that's what the note at the end of my answer is all about =) – Josh Darnell Oct 19 '11 at 13:14
  • 1
    This will measure the text size on current Form. If you need to measure the text size for example on printer output, you will need to use `e.Graphics.MeasureString(...)` method. – AaA Jan 30 '18 at 08:21
13

You can get a Graphics object (using Control.CreateGraphics() on the container you intend the text for) and call MeasureString() to do this. It's a fairly common GDI+ technique.

More information from MSDN: http://msdn.microsoft.com/en-us/library/6xe5hazb.aspx

Neil Barnwell
  • 41,080
  • 29
  • 148
  • 220
  • AFAIK GDI+ text measurements are a bit problematic. In particular when using the size to center text that doesn't work as desired. – CodesInChaos Oct 10 '11 at 14:18
  • I use the `MeasureString` method and now I got a return value with type of SizeF then got it to PointF and I know now the width but in PointF >>> I need it in cm ! I try to convert it as I think the PointF is like the Postscript point but I think my converting formula is not correct >> so How to convert after get the poinf width !? – AshOoO Oct 11 '11 at 11:20
  • 2
    `PointF` is just a `float` value for a `Point` (i.e. non-integral values). Converting from points to centimetres should be doable (points->pixels->dpi->inches->cm?) but why do you need centimetres? – Neil Barnwell Oct 11 '11 at 11:44
  • My actual case is that I need to draw a string in a specific rectangle width ,So I need to check first if my string fit the rectangle width as if not it will wrap but in a formated way – AshOoO Oct 11 '11 at 12:34
3

You can use this:

private float getTextSize(string text)
    {
        Font font = new Font("Courier New", 10.0F);
        Image fakeImage = new Bitmap(1, 1);
        Graphics graphics = Graphics.FromImage(fakeImage);
        SizeF size = graphics.MeasureString(text, font);
        return size.Width;
    }