I'm trying to estimate the widths in pixel if a text would be rendered in Chrome by using C# for a specific font (Arial 18px) in a tool that I'm creating.
Comparing my results with this tool (uses the browser to render the width): http://searchwilderness.com/tools/pixel-length/ the string:
"Lorem ipsum dolor sit amet, consectetur adipiscing elit."
Is calculated to be 439 pixels wide.
But with this code in C# I get 445px:
var font = new Font("Arial", 18, FontStyle.Regular, GraphicsUnit.Pixel);
var text = "Lorem ipsum dolor sit amet, consectetur adipiscing elit.";
var size = TextRenderer.MeasureText(text, font, new Size(int.MaxValue, int.MaxValue), TextFormatFlags.NoPadding);
Can I modify my code so it renders similar to the browser?
I've tried to output a label with the font and text and compared with browser rendering they do match (+/- 1px).