How do I turn off text padding inside layout rectangle performed by Graphics.DrawString?

1.7k views Asked by At

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?

1

There are 1 answers

1
falstaff On

You can resolve this by using GenericTypographic:

...
g.DrawString("Hi", font, Brushes.Black, 0, 0, StringFormat.GenericTypographic);
...