Click here to Skip to main content
13,196,464 members (49,099 online)
Click here to Skip to main content
Add your own
alternative version


21 bookmarked
Posted 29 Sep 2004

Measuring strings

, 29 Sep 2004
Rate this:
Please Sign up or sign in to vote.
An example of using the Graphics.MeasureString() method.


Suppose you have a list of companies in a database table. Someone asked you to print each company name on a different label, using a specified font and font size. You have to be sure that all the company names will fit into the labels, without any word-wrap; you can introduce abbreviations if a company name doesn't fit.

The font you are using is not a fixed-length font but a proportional font, so the character length of each string cannot be used to anticipate the real printed length of the string. How can you do?

You simply have to "measure" each string, computing which width it will have when printed in the specified font and with the specified font size. To do this, you can use the MeasureString() method of the .NET Framework Graphics class.

The application

The simple application I wrote helps you in "measuring" strings, given a font and a font size. The core measuring functionality is in the Measure() subroutine:

Private Sub Measure(ByVal BannerText As String, _
      ByVal FontName As String, ByVal FontSize As Single, _
      ByRef Width As Single, ByRef Height As Single)

    Dim b As Bitmap
    Dim g As Graphics
    Dim f As New Font(FontName, FontSize)

    ' Compute the string dimensions in the given font
    b = New Bitmap(1, 1, PixelFormat.Format32bppArgb)
    g = Graphics.FromImage(b)
    Dim stringSize As SizeF = g.MeasureString(BannerText, f)
    Width = stringSize.Width
    Height = stringSize.Height

End Sub

How to use the application:

  • once the application is running, choose your preferred font, clicking on the "Set Font" button;
  • type in the upper textbox one of the longest strings permitted by your label sizes (you have to do a trial print, of course);
  • in the "Current width" box, you will read the width (in pixel) of the typed string (computed for the given font and font size): this is the maximum width allowed by your label, so you can mark it clicking on the "Set Limit" button;
  • from now on, all the new strings you will type in the textbox will be measured and their width will be compared with the limit you set, giving you evidence for strings that are "too long", and allowing you to modify them (introducing abbreviations) until their width will fit the maximum allowed width.

Strings Meter user interface

Of course, this application is only an example. In a real situation, you will have to integrate a subroutine like Measure() inside a program that will: fetch your data from the database table; measure the string; warn you about "too long" strings and let you modify them, storing them again in the data source.


This article has no explicit license attached to it but may contain usage terms in the article text or the download files themselves. If in doubt please contact the author via the discussion board below.

A list of licenses authors might use can be found here


About the Author

Alberto Venditti
Technical Lead
Italy Italy
I was born in 1970.

My first computer experience dates back to early 80s, with a Sinclair ZX81.
From that time on, as many "friends" say, my IT-illness has increased year by year.

I graduated in Electronic Engineering and earned the following Microsoft certifications:
MCP, MCT, MCDBA, MCSD, MCAD, MCSD for .NET (early achiever).

I worked in IT as a developer, a teacher, a consultant, a technical writer, a technical leader.
IT knowledge applied to real life is my primary interest and focus.

You may also be interested in...

Comments and Discussions

GeneralMy vote of 5 Pin
jasonalls21-Sep-12 0:45
memberjasonalls21-Sep-12 0:45 
GeneralMy vote of 1 Pin
rohitkandhal20-Sep-10 1:54
memberrohitkandhal20-Sep-10 1:54 
Generalgood work Pin
deltalmg2-Oct-07 7:21
memberdeltalmg2-Oct-07 7:21 
GeneralNot accurate Pin
adamdev30-Sep-04 0:54
memberadamdev30-Sep-04 0:54 
GeneralRe: Not accurate Pin
Alberto Venditti1-Oct-04 10:51
memberAlberto Venditti1-Oct-04 10:51 
GeneralRe: Not accurate Pin
Alberto Venditti1-Oct-04 10:55
memberAlberto Venditti1-Oct-04 10:55 
GeneralRe: Not accurate Pin
Labrat00212-Jan-05 11:40
memberLabrat00212-Jan-05 11:40 
GeneralRe: Not accurate Pin
FernandaUY23-Dec-11 12:12
memberFernandaUY23-Dec-11 12:12 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.

Permalink | Advertise | Privacy | Terms of Use | Mobile
Web03 | 2.8.171019.1 | Last Updated 29 Sep 2004
Article Copyright 2004 by Alberto Venditti
Everything else Copyright © CodeProject, 1999-2017
Layout: fixed | fluid