Drawing trimmed text with ellipsis …
C#, Graphics and Images February 22nd, 2006I have been writing a mulit-columnal control that rendered it’s text using the graphics.DrawString method and formatted to add the ellipsis characters if the text was too long for the output rect. However, as the column was resized, the text inter-character spacing changed, giving a very disconcerting wobble to the text.
To correctly output trimmed text I had to use the TextRenderer.DrawText method.
[csharp]
TextFormatFlags flags = new TextFormatFlags();
flags = TextFormatFlags.EndEllipsis | TextFormatFlags.Left | TextFormatFlags.VerticalCenter ;
TextRenderer.DrawText(g, text, font, rect, SystemColors.ControlText,flags);
[/csharp>
Please Note : microsoft recommend using TextRedner for drawing text…
System.Drawing.Graphics class presents some limitations—it is based on GDI+ and currently has limited support for complex scripts. For more information see MSDN article http://msdn.microsoft.com/msdnmag/issues/06/03/TextRendering/default.aspx
February 28th, 2006 at 12:10 pm
TextRender dows not seem to honour the trasnfromations applied to tge Grphics object. Looking into it.
March 3rd, 2006 at 12:59 pm
To preserve any translations or clipping regions on the graphics surface, add the following to the TextFormatFlags.
TextFormatFlags.PreserveGraphicsTranslateTransform | TextFormatFlags.PreserveGraphicsClipping