3

I get the x and y coordinates for a text from a Java backend. Now I want to draw that font in C# (Graphics.DrawString) the exact way as Java does it. For that I have to calculate the baseline. I have a code that makes it kind of right for some fonts, but is not rendered correctly for other fonts.

SizeF size = graphics.MeasureString(str, font);                                  
int descent = font.FontFamily.GetCellDescent(font.Style);
float factor = font.Size / font.FontFamily.GetEmHeight(font.Style);
float leadingCalc =  (ascent * factor + descent * factor) - emHeight * factor;
graphics.DrawString(str + "", font, brush, x, y - font.Height + descent * factor + leadingCalc);   

The question is, does someone know are more precise or better way to calculate the baseline?

Cœur
  • 37,241
  • 25
  • 195
  • 267
Michael S
  • 221
  • 2
  • 10
  • possible duplicate of [How do I get the position of the text baseline in a Label and a NumericUpDown?](http://stackoverflow.com/questions/1006069/how-do-i-get-the-position-of-the-text-baseline-in-a-label-and-a-numericupdown) – Pete Kirkham Jan 29 '13 at 20:28

0 Answers0