Question

I need to create bitmaps of characters depending on a specified font. When they specify a font, they are specifying:

  • Font (e.g. Microsoft Sans Serif)
  • Font style (e.g. Bold)
  • Size (e.g. 14)
  • Effects (e.g. Stikeout)
  • Script (e.g. Western)

Knowing this, is it possible to determine the size that a character will be exactly if I know the character and all the information above? I have to draw them to bitmaps that are the same size as the character and no bigger.

Thanks! I'm doing this all in VB.net so all .net examples are acceptable.

Was it helpful?

Solution

You need to use the Graphics.MeasureString() method. Create the graphics object for the form (or other graphics output object) and the font and use them to measure the text.

Public Sub Form1_Shown(sender As Object, e As EventArgs) Handles Me.Shown
    Using _
        graphics As Graphics = Me.CreateGraphics, _
        font As New Font("Microsoft Sans Serif", 14, FontStyle.Bold Or FontStyle.Strikeout)

        Dim text As String = "How big am I?"
        Dim size As SizeF = graphics.MeasureString(text, font)
        MessageBox.Show(size.ToString)
    End Using
End Sub
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top