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
3
  • Try the different options of Graphics.TextRenderingHint. The anti-alias or ClearType options should help. Commented Jul 5, 2012 at 6:58
  • Actually, I had tried AntiAliasGridFit, ClearTypeGridFit and the SingleBitPerPixel options before but obviously missed AntiAlias. Using AntiAlias does the trick, thanks! Commented Jul 5, 2012 at 7:24
  • PS: If you like to post your answer I'll gladly accept it Commented Jul 5, 2012 at 8:27

1 Answer 1

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.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.