3

When I write a string with Graphics.DrawString, passing a StringFormat object (carrying StringAlignment.Near) and a layout Rectangle, the text starts some number of pixels to the right of the edge of the Rectangle. This padding is related to size of the Font used, so it makes it impossible to programmatically align strings written in different sizes.

Is there a way to avoid this, and start the text immediately at X=0 relative to the layout rectangle?

sehrgut
  • 209
  • 1
  • 13

1 Answers1

2

You can resolve this by using GenericTypographic:

...
g.DrawString("Hi", font, Brushes.Black, 0, 0, StringFormat.GenericTypographic);
...
falstaff
  • 3,413
  • 2
  • 25
  • 26
  • `StringFormat.GenericTypographic` doesn't reduce the padding box. If you run your code multiple times with different sized fonts, you'll see the strings in the larger fonts are written beginning further to the right than strings written in the smaller fonts. – sehrgut Jan 17 '14 at 15:55