Search code examples
c#.netvb.netgraphicsmeasurestring

Generic function to return String measure (Graphics.MeasureString)


In C# or VB.NET how I could write a generic function which only should need to pass a string and a font to return the measure of the font?

This is What I've tried:

Private Function Get_Text_Measure(ByVal text As String, _
                                  ByVal font As Font) As SizeF

    Using g As Graphics = ...
        Return g.MeasureString(text, font)
    End Using

End Function

Solution

  • You can use TextRenderer

    return TextRenderer.MeasureText(text , font);