I am using pixels as the unit for my font. In one place, I am performing a hit test to check if the user has clicked within the bounding rectangle of some text on screen. I
MeasureString
method in @NerdFury answer will give a higher string width than expected.Additional info you can find here. If you want to measure only the physical length please add this two lines :
g.TextRenderingHint = System.Drawing.Text.TextRenderingHint.AntiAlias;
result =
g.MeasureString(measuredString, font, int.MaxValue, StringFormat.GenericTypographic);