1

I'm using the classic DrawString method to output text in a WinForms app. A typical call look like this:

g.DrawString(text, font, brush, new Rectangle(x, y, width, height), stringFormat);

If stringFormat.Trimming equals StringTrimming.EllipsisCharacter, the text suddenly "jumps" 1 pixel up if it is clipped and the rectangle of the same left/top/height is used:

enter image description here

This happens for many standards fonts like MS Sans Serif or Courier New, but does not happen for others like Segoe UI. And what is more strange, we can avoid this effect if we specify StringFormatFlags.DirectionRightToLeft for stringFormat.FormatFlags.

Is it a known issue of GDI+, and is there a workaround for that?

P.S. Tested all this in Win 8.1 Pro 64-bit, in an app compiled for .NET 4.0.

TecMan
  • 2,743
  • 2
  • 30
  • 64
  • 1
    Graphics.DrawString() is notorious for problems like this. Microsoft's first attempt at resolution-independent text rendering, it didn't pan out so well. Which is why you need to use the TextRenderer class instead. – Hans Passant Dec 31 '14 at 11:48
  • @HansPassant, we use different [StringFormat](http://msdn.microsoft.com/en-us/library/system.drawing.stringformat%28v=vs.110%29.aspx) options with `Graphics.DrawString` (vertical text alignment, end ellipsis, etc). Do you think we can have all this when we switch to `TextRenderer.DrawText`? For instance, MSDN states that the `TextFormatFlags.Bottom` option can be "applied only when the text is a single line". And what about printing using the standard .NET `PrintDocument` class - it seems the existing drawing code will stop working (printing) after switching to TextRenderer? – TecMan Jan 12 '15 at 10:24

0 Answers0