Question

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
Was it helpful?

Solution

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.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top