Browse DevX
Sign up for e-mail newsletters from DevX

Tip of the Day
Language: VB7
Expertise: beginner
Feb 17, 2003



Building the Right Environment to Support AI, Machine Learning and Deep Learning

Measuring a string drawn with a specific font

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 bitmap
Dim bmp As New Bitmap(400, 200, Drawing.Imaging.PixelFormat.Format16bppRgb565)
' get the underlying Graphics object
Dim gr As Graphics = Graphics.FromImage(bmp)
' measure the string
Dim 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.
Marco Bellinaso
Comment and Contribute






(Maximum characters: 1200). You have 1200 characters left.



Thanks for your registration, follow us on our social networks to keep up-to-date