79663965

Date: 2025-06-12 18:28:50
Score: 0.5
Natty:
Report link

Wow, these answers are more complex than I wanted... It's a bit of a hack and the numbers I used were arrived at through an iterative process... I'm using a monospaced font so all of my characters should be the same width - surprise, a space has 0 base width + the added amount before and after.

So... Measure the width of a single character and then adjust based on the length of the string.

using (Graphics g = Graphics.FromImage(MyImage))
var stringsize = g.MeasureString("M", MyFont);
stringsize.Width = stringsize.Width * ((float)0.92 + (txt.Length - 1) * (float)0.65);
        //0.92 for the first character, 0.65 for everything after
Reasons:
  • Long answer (-0.5):
  • Has code block (-0.5):
  • Unregistered user (0.5):
  • Low reputation (1):
Posted by: Robert