Groups | Blog | Home
all groups > dotnet drawing api > january 2007 >

dotnet drawing api : ClearType is not used with Vertical text


ljlevend2
1/26/2007 3:25:01 PM
I've noticed that Drawing.Graphics.TextRenderingHint = ClearTypeGridFit does
not seem to be used when StringFormat.FormatFlags = DirectionVertical. This
is best seen by running the following example:


Public Class Form1
Inherits Windows.Forms.Form

Protected Overrides Sub OnSizeChanged(ByVal e As System.EventArgs)
MyBase.OnSizeChanged(e)
Me.Invalidate()
End Sub

Protected Overrides Sub OnPaint(ByVal e As
System.Windows.Forms.PaintEventArgs)
MyBase.OnPaint(e)

Const text As String = "VWXYZ"
Dim color As Drawing.Color = color.Black
Dim bounds1 As New Drawing.Rectangle(0, 0, (Me.ClientSize.Width \ 3),
Me.ClientSize.Height)
Dim bounds2 As New Drawing.Rectangle(bounds1.Right, 0, bounds1.Width,
Me.ClientSize.Height)
Dim bounds3 As New Drawing.Rectangle(bounds2.Right, 0, bounds1.Width,
Me.ClientSize.Height)
Dim font As New Drawing.Font("Arial", 12, Drawing.FontStyle.Regular,
Drawing.GraphicsUnit.Point) 'Use a font that supports ClearType.

'Technique #1: Draw directly.
Using brush As New Drawing.SolidBrush(color)
Dim stringFormat As New Drawing.StringFormat()
stringFormat.Alignment = Drawing.StringAlignment.Center
stringFormat.LineAlignment = Drawing.StringAlignment.Center
stringFormat.FormatFlags = Drawing.StringFormatFlags.DirectionVertical
Dim temp As Drawing.Text.TextRenderingHint =
e.Graphics.TextRenderingHint
e.Graphics.TextRenderingHint =
Drawing.Text.TextRenderingHint.ClearTypeGridFit 'Make sure that we are using
ClearType.
e.Graphics.DrawString(text, font, brush, bounds1, stringFormat)
e.Graphics.TextRenderingHint = temp
End Using

'Technique #2: Draw to an image, rotate the image and then draw the image.
Dim imgBounds2 As New Drawing.Rectangle(0, 0, bounds2.Height,
bounds2.Width)
Using img As New Drawing.Bitmap(imgBounds2.Width, imgBounds2.Height,
Drawing.Imaging.PixelFormat.Format32bppArgb)
Using gImg As Drawing.Graphics = Drawing.Graphics.FromImage(img)
gImg.TextRenderingHint =
Drawing.Text.TextRenderingHint.ClearTypeGridFit 'Make sure that we are using
ClearType.
'gImg.Clear(Me.BackColor)
Using brush As New Drawing.SolidBrush(color)
Dim stringFormat As New Drawing.StringFormat()
stringFormat.Alignment = Drawing.StringAlignment.Center
stringFormat.LineAlignment = Drawing.StringAlignment.Center
gImg.DrawString(text, font, brush, imgBounds2, stringFormat)
End Using
End Using
img.RotateFlip(Drawing.RotateFlipType.Rotate90FlipNone)
e.Graphics.DrawImageUnscaled(img, bounds2.Location)
End Using

'Technique #3: Create an image of the background, draw to the image,
rotate the image and then draw the image.
'NOTE: The problem with this technique is that we often don't know what
the background is when the text is drawn.
Dim imgBounds3 As New Drawing.Rectangle(0, 0, bounds3.Height,
bounds3.Width)
Using img As New Drawing.Bitmap(imgBounds3.Width, imgBounds3.Height,
Drawing.Imaging.PixelFormat.Format32bppArgb)
Using gImg As Drawing.Graphics = Drawing.Graphics.FromImage(img)
gImg.TextRenderingHint =
Drawing.Text.TextRenderingHint.ClearTypeGridFit 'Make sure that we are using
ClearType.
gImg.Clear(Me.BackColor) 'This effectively draws the background.
Using brush As New Drawing.SolidBrush(color)
Dim stringFormat As New Drawing.StringFormat()
stringFormat.Alignment = Drawing.StringAlignment.Center
stringFormat.LineAlignment = Drawing.StringAlignment.Center
gImg.DrawString(text, font, brush, imgBounds3, stringFormat)
End Using
End Using
img.RotateFlip(Drawing.RotateFlipType.Rotate90FlipNone)
e.Graphics.DrawImageUnscaled(img, bounds3.Location)
End Using
End Sub
End Class


When you show the form you will see three vertically drawn strings. The
left string is drawn using Technique1 which is the technique that I want, but
ClearType does not seem to be used. The middle string is drawn using
Technique2 which is to draw horizontally to a bitmap and then rotate the
bitmap. Technique2 would also be acceptable except that the resulting text
looks poor. The right string is drawn using Technique3 which is the same as
Technique2 except that the background of the control is drawn to the bitmap
first. The text looks good when Technique3 is used, but the technique
requires us to know what the background should look like which is often
difficult or impossible to determine.

A good example of when it would be difficult to implement Technique3 is if
we inherit from Windows.Forms.ToolStripProfessionalRenderer in order to
custom draw menu items. That class provides the OnRenderMenuItemBackground
and OnRenderItemText for drawing the background and text, respectively.
Drawing text in OnRenderMenuItemBackground would be difficult because the
TextRectangle is not specified to that method. On the otherhand, drawing the
background in OnRenderItemText might cause problems because there is the
chance that we would draw over the image (if OnRenderItemText is called after
OnRenderItemImage).

So, what I am looking for is a way to have vertically drawn text use
ClearType without any knowledge of what the background should look like.

Thanks for any help!
Lance
Bob Powell [MVP]
1/30/2007 10:20:40 AM
When I modified my "Text at any angle" code to use various sorts of
antialias, gridfit and cleartype options I found that all the text changed.

This doesn't use the StringFormat flags option and I think that this is
a special case.

Perhaps you can work around by simply drawing the text at the angle you
want using the appropriate settings.

http://www.bobpowell.net/angletext.htm

--
Bob Powell [MVP]
Visual C#, System.Drawing

Ramuseco Limited .NET consulting
http://www.ramuseco.com

Find great Windows Forms articles in Windows Forms Tips and Tricks
http://www.bobpowell.net/tipstricks.htm

Answer those GDI+ questions with the GDI+ FAQ
http://www.bobpowell.net/faqmain.htm

All new articles provide code in C# and VB.NET.
Subscribe to the RSS feeds provided and never miss a new article.





[quoted text, click to view]
ljlevend2
1/30/2007 6:29:00 PM
Hi Bob,

Thanks a lot for the idea. I am swamped for the next couple of days, but
I'll give it a try as soon as I get a chance and reply back with the results.

Thanks again,
Lance
AddThis Social Bookmark Button