When you’re dynamically drawing text (either on a Windows Form or Web Form application), you may need to calculate the width and height of a string printed with a particular font. This is easy with the Graphics class’s Measure string method, that takes in input the string being measured, and the font it will be printed with. Follows an example:
' create a new bitmapDim bmp As New Bitmap(400, 200, Drawing.Imaging.PixelFormat.Format16bppRgb565)' get the underlying Graphics objectDim gr As Graphics = Graphics.FromImage(bmp)' measure the stringDim stringSize As SizeF = gr.MeasureString("Test string", _ New Font("Courier New", 18, FontStyle.Bold, GraphicsUnit.Pixel))
Finally you can read the Height and Width properties of the returned SizeF variable (stringSize) in the example above.