1

This is possibly a duplicate question, but I am unable to find the answer.

My only question is how to get height (ascent + descent, without any spacing) of the Font used in WinForms in pixels. I have Segoe UI 8.25 Regular font.

Font.Height gives me 15 (pixels?)

I am able to get font metrics ans use FontFamily:

  • GetEmHeight gives me 2048.
  • GetCellAscent gives me 2210 (larger than EM?)
  • GetCellDescent gives me 514

According to formula from MSDN

I should do following:

(ascent + descent) * font.Height / emHeight
which is
(514 + 2210) * 15 / 2210 = 19 pixels.

So:

  • Calculating using MSDN's formulas gives me 19px text height.
  • TextRenderer.MeasureText for "Tg" string gives me 13px.
  • If a take a screenshot and measure it in Photoshop (from lowest bound of g to highest bound of T) i'll receive 10px.

What I'm doing wrong and how this values are interconnected? The goal is to get something as closer as possible to screenshot's measuring results.

TextRenderer.MeasureText uses much of resources and I can't use it anytime during draw.

rattler
  • 379
  • 2
  • 5
  • 15
  • 2
    What are you trying to achieve ? – aybe Mar 11 '18 at 16:20
  • To get pixels, you use conversion formula. – Héctor M. Mar 11 '18 at 16:24
  • @Aybe just want to know the height of text for render calculations. If the text is definitely single-lined, there is no need to calculate the wordwrap (which is resource extensive) and the font's height is enough. – rattler Mar 11 '18 at 16:42
  • a) did you read about the internal leading? b) did you include anti-aliased pixel in your photoshop measuring? c) why do you worry about performance?? You can measure before drawing and use the pre-calculated data, no? d) you can also use a graphicspath's bounds to measure oly colored pixels.. – TaW Mar 11 '18 at 16:45
  • 2
    You are using Font.Height where your reference is using Font.Size. – TnTinMn Mar 11 '18 at 16:48
  • @TaW a) i don't need any internal leading. thats why i use `TextRenderer.MeasureText` instead of `Graphics.MeasureString` b) as I saw in photoshop, anti-aliasing is applied only in horizontal direction. and even if i add one pixel per each side, i should get 12px, not 19. c) I need.. to cache the Font height every time it is changed? It may work only as last chance hack. I don't need to measure strings in this case. I need only font's height.. – rattler Mar 11 '18 at 16:49
  • @TnTinMn, thanks. It seems, that this is it. using `Size` gives 10.9px which is close enough to desired. This is the answer for what im doing wrong. You may write it as an answer and i'll accept it. – rattler Mar 11 '18 at 16:51
  • 2
    It is the same as the answer from @HéctorManuelMartinezDurán, to whom you owe an apology for your comment to his answer. – TnTinMn Mar 11 '18 at 16:55
  • The link makes a difference betqween internal and external leading; just want to point that out. Wow, you are right about anti-aliasing going only horizontally! Interesting.. – TaW Mar 11 '18 at 16:56
  • Answer of @HéctorManuelMartinezDurán just references the article from MSDN I already read, while your answer points me on my mistake. Thats the difference – rattler Mar 11 '18 at 16:56
  • No, his answer shows using Font.Size. – TnTinMn Mar 11 '18 at 16:57
  • Hmm. My question is "I used the formula from article and get wrong result. WTF?". He answers "use the formula from article". I don't think it is the answer for the question WTF:) Your answer is "you are using it incorrecly". This clearly answers for the question WTF. – rattler Mar 11 '18 at 17:03
  • There seems to be a problem with "Segoe UI" font family - using size 8.25 / regular style I get GetLineSpacing: 2724, GetCellAscent: 2210, GetCellDescent: 514 - you'll notice that LineSpacing = Ascent + Descent. So where has the gap gone? – Gwynge Jun 21 '19 at 18:45

1 Answers1

6

To get pixels, you use conversion formula:

descentPixel = font.Size * descent / fontFamily.GetEmHeight(FontStyle.Regular);

From this link https://learn.microsoft.com/en-us/dotnet/framework/winforms/advanced/how-to-obtain-font-metrics

TaW
  • 53,122
  • 8
  • 69
  • 111
Héctor M.
  • 2,302
  • 4
  • 17
  • 35
  • 3
    Please read question carefully. I used that formula and get almost twice of required height. 19 px instead of 10 – rattler Mar 11 '18 at 16:40