[RESOLVED] Graphics.RotateTransform for vertical aligned Text
Hi All,
I'm using the following calls in a vertical label control: (borrowed from another project)
Code:
vlblControlWidth = this.Size.Width
lblControlHeight = this.Size.Height
e.Graphics.DrawRectangle(labelBorderPen, 0, 0, lblControlWidth,vlblControlHeight)
e.Graphics.FillRectangle(labelBackColorBrush, 0, 0, vlblControlWidth,vlblControlHeight)
e.Graphics.TextRenderingHint = System.Drawing.Text.TextRenderingHint.ClearTypeGridFit
e.Graphics.SmoothingMode = System.Drawing.Drawing2D.SmoothingMode.HighQuality
e.Graphics.InterpolationMode = System.Drawing.Drawing2D.InterpolationMode.HighQualityBicubic
vlblTransformX = 0
vblTransformY = vlblControlHeight
e.Graphics.TranslateTransform(vlblTransformX, vlblTransformY)
e.Graphics.RotateTransform(270)
e.Graphics.DrawString(labelText, Font, labelForeColorBrush, 0, 0)
The label control draws properly etc.
But if any of the text characters are a V or W there are funny jagged artifacts in those letter's angled lines.
Does anyone know how to fix this? I tried looking up anti-aliasing etc.. but aside from changing the smoothing mode to antialias I couldn't find anything (changing this doesn't help a bit)
Thanks,
Nick
Re: Graphics.RotateTransform for vertical aligned Text
Set the Graphics.TextRenderingHint to AntiAliasGridFit. That smooths the results of DrawString. The SmoothingMode is for drawn lines (e.g. DrawLine) and edges (e.g. FillEllipse). BB
EDIT: Now I see you are already using ClearTypeGridFit. That ought to provide antialiasing too. You could try alternative settings, but maybe we have to look for another cause for the blocky edges.
3 Attachment(s)
Re: Graphics.RotateTransform for vertical aligned Text
Here is the full code if it would help.
*Edit* translated to VB.net
Re: Graphics.RotateTransform for vertical aligned Text
You came too late with the VB version, but never mind, I braved the shark-infested waters of Csharp to take a look. TextRenderingHint.Antialias (without GridFit!) seems to give better diagonals and curves, but the straight lines become slightly irregular (on my screen). Sans-Serif fonts do better than Serif ones such as in your post above. Making the font slightly larger helps a lot -- 9 points is enough for Microsoft Sans Serif. I suspect that this is as good as you are going to get in WinForms. FWIW, WPF is reputed to be better at sub-pixel rendering. BB
Re: Graphics.RotateTransform for vertical aligned Text
Thanks BB!
I think you have the right of it; I'm going to leave this open for a bit more to see if anyone else has any other insight.
I'll look into WPF.
- Nick
Re: Graphics.RotateTransform for vertical aligned Text
I can think of a couple of other possibilities that would be interesting to experiment with, although I don't have time at the moment,
1. Create a text path with GraphicsPath.AddString, rotate the path with its own matrix argument, then render the text with Graphics.Drawpath or Graphics.FillPath.
2. Render the text horizontally to a bitmap, then draw the bitmap rotated with Graphics.DrawImage.
3. Render the text to a bitmap at double the target size, then draw the bitmap reduced to the target size with Graphics.DrawImage.
I would be surprised if 1 offers much if any benefit, but I think 2 combined with 3 could well give an improved result for small text.
BB
Re: Graphics.RotateTransform for vertical aligned Text
I've not had much success with vertical rendering of text, not that I have done it much. However, my preference is to render text to an image and draw a rotated image. It seems to work for me, anyway, since I'm not a big fan of vertical text so don't do much of it.
Re: Graphics.RotateTransform for vertical aligned Text
Hi all; it seems that writing to a bitmap and using the bitmap.rotate method seems to work much better... I can't for the life imagine why it would be different but it is. Cheers! Thanks!