2

Microsoft Office does a very nice job when it comes to scaling of fonts. They can almost linearly be scaled in steps of 0.5 points (see image below).

Using Graphics.DrawString I was not able to reproduce this but I see distinct steps when scaling the font size.

With the code below I get the following output which shows that I cannot draw text in as many sizes as Office can. Any ideas how I can draw those intermediate font sizes?

Font size comparison

    Dim baseSize As Single = 16.0F
    Dim inputText As String = "MMMMMMMMMMMMMM"

    Dim stringFormat As Drawing.StringFormat = Drawing.StringFormat.GenericTypographic()

    Dim pos As Single
    Dim i As Integer

    Do
        Using font As Drawing.Font = New Drawing.Font("Calibri", (baseSize + i / 10.0F), FontStyle.Regular, GraphicsUnit.Pixel)
            Dim text As String = inputText & " " & font.Size.ToString() & "px"
            Dim textSize As SizeF = e.Graphics.MeasureString(text, font, New PointF(0, 0), stringFormat)
            e.Graphics.DrawString(text, font, Brushes.Black, New Drawing.RectangleF(10, pos, textSize.Width, textSize.Height), stringFormat)
            pos += font.Height
        End Using
        i += 1
    Loop While pos < ClientRectangle.Height
Paul B.
  • 2,394
  • 27
  • 47
  • Try the different options of [Graphics.TextRenderingHint](http://msdn.microsoft.com/en-us/library/system.drawing.graphics.textrenderinghint). The anti-alias or ClearType options should help. – Codo Jul 05 '12 at 06:58
  • Actually, I had tried AntiAliasGridFit, ClearTypeGridFit and the SingleBitPerPixel options before but obviously missed AntiAlias. Using AntiAlias does the trick, thanks! – Paul B. Jul 05 '12 at 07:24
  • PS: If you like to post your answer I'll gladly accept it – Paul B. Jul 05 '12 at 08:27

1 Answers1

1

Set the text rendering hint:

e.Graphics.TextRenderingHint = TextRenderingHint.AntiAlias;

Anti-alias is required for almost continuous scaling. But - juding from your comment - grid-fitting causes the font-size to be rounded to some discrete value that prevents it.

Codo
  • 75,595
  • 17
  • 168
  • 206