Measure the height and width of a string in VB.NET

Use the Graphics.MeasureString method exposed by the PaintEventArgs parameter of a form or control's Paint event or the PrintPageEventArgs parameter of the PrintDocument object's PrintPage event.

MeasureString measures a string when drawn with a specified font and returns a SizeF structure which contains height and width values in pixels.

    Private Sub Form1_Paint(ByVal sender As Object, _
        ByVal e As System.Windows.Forms.PaintEventArgs) Handles MyBase.Paint

        Dim myFont As New Font("Courier New", 8)
        Dim myFontBold As New Font("Microsoft Sans Serif", 10, FontStyle.Bold)
        Dim StringSize As New SizeF

        StringSize = e.Graphics.MeasureString("How wide is this string?", myFont)
        Debug.WriteLine("Height: " & StringSize.Height)
        Debug.WriteLine("Width: " & StringSize.Width)

        StringSize = e.Graphics.MeasureString("How wide is this string?", myFontBold)
        Debug.WriteLine("Height: " & StringSize.Height)
        Debug.WriteLine("Width: " & StringSize.Width)
    End Sub

You can use MeasureString to calculate how many characters will fit within a specified StringFormat object. A StringFormat object encapsulates text layout information such as alignment and line spacing as well as font information.

    Private Sub Form1_Paint(ByVal sender As Object, _
        ByVal e As System.Windows.Forms.PaintEventArgs) Handles MyBase.Paint

        Dim myFont As New Font("Courier New", 8)
        Dim strString1 As String = "How wide is this string?"
        Dim strString2 As String = "How much of this string will fit within strString1?"
        Dim myFontBold As New Font("Microsoft Sans Serif", 10, FontStyle.Bold)
        Dim StringSize As New SizeF
        Dim LayoutArea As New SizeF
        Dim NewStringFormat As New StringFormat
        Dim intLinesFilled As Integer
        Dim intCharactersFitted As Integer
        '
        ' Measure string 1's height and width.
        '
        StringSize = e.Graphics.MeasureString(strString1, myFont)
        '
        ' Create a StringFormat object specifying not to wrap text.
        '
        NewStringFormat.FormatFlags = StringFormatFlags.NoWrap
        '
        ' Create a structure specifying the maximum layout area for the string. Set the
        ' width to the width of string 1 and the height to the textheight of the font used.
        '
        LayoutArea.Width = StringSize.Width
        LayoutArea.Height = myFont.GetHeight(e.Graphics)
        '
        ' See how many characters of string 2 fit within the layout area (width of string 1).
        '
        StringSize = e.Graphics.MeasureString(strString2, myFont, _
                      LayoutArea, NewStringFormat, intCharactersFitted, intLinesFilled)

        Debug.WriteLine(intCharactersFitted)
    End Sub




About TheScarms
About TheScarms


Sample code
version info

If you use this code, please mention "www.TheScarms.com"

Email this page


© Copyright 2024 TheScarms
Goto top of page