0

When drawing a string I get the required size by calling myGraphics.MeasureString(myString, myFont);. Experimentally I found out that this method always returns the same height for any string or single character of a certain font. So this value seems to be a feature of the font and not a feature of the string. But there is no property or method in the font class which delivers this information. The font class has several properties / methods returning height information but none of them gives the same value as the string measurement. Example:

using (Graphics myGraphicsTemp = CreateGraphics())
{               
       Font myFont = new Font("Microsoft Sans Serif", 9F);
       var size = myFont.Size;           //  9
       var height = myFont.Height;       // 14
       var lineSpacing = myFont.GetHeight();  // 13.5820293
       var measuredHeight = myGraphicsTemp.MeasureString("1", myFont).Height; // 15.0820284
}

I need the height given by the string measuring method at several places in my code. So I set a variable (like measuredHeight in my example) by measuring an arbitrarily chosen character. That works but I think this has a "strong smell". Is there a better way to find the required value? I found the following question (Width and height of font) but it did not answer my question.

Elec1
  • 235
  • 2
  • 8
  • Do you mean that the height of `MeasureString` is always the same? Since the width should be depending on the string length. – JonasH Jul 29 '21 at 09:05
  • @JonasH : No. The width of the string depends of course on the string. But the height is always the same. I did test that for several different examples (lowercase letters, capital lettters, characters with descenders (e.g. g), numbers, multi letter strings). – Elec1 Jul 29 '21 at 09:27
  • I think that is expected, and I do not know if any better way to get the font-height than to call MeasureString. – JonasH Jul 29 '21 at 09:29
  • @JonasH The problem I see with this approach is that one relies on an undocumented and may be even undefined behaviour. – Elec1 Jul 29 '21 at 09:48
  • I would not think this would be "undefined behavior" since that usually implies "anything can happen". While I agree that it is undocumented behavior, I would not be to worried, since it would almost certainly break other applications if the behavior changed. But if you or someone else knows of a better way I would be glad to know about it. – JonasH Jul 29 '21 at 10:02
  • 1
    See the notes (and links) here: [Properly draw text using GraphicsPath](https://stackoverflow.com/a/53074638/7444103) -- [How to compute the correct width of a digit in pixels?](https://stackoverflow.com/a/54772134/7444103) -- [Remove top and bottom padding from Text drawn on an Image](https://stackoverflow.com/a/54383828/7444103) – Jimi Jul 29 '21 at 10:23
  • 1
    The notes here: [Drawing a Long String on to a Bitmap results in Drawing Issues](https://stackoverflow.com/a/49953353/7444103), in relation to the difference between GDI+ and GDI rendering could also be of interest. – Jimi Jul 29 '21 at 10:29
  • 1
    @Jimi Thank you very much for the heap of information you provided. Gives me lot of stuff to play with. – Elec1 Jul 29 '21 at 12:58

0 Answers0