Sometimes we need to know the width of a given string in pixels, do you know how to compute it? Before writing some long code, please notice that the .NET framework class library provides such a method. When Googling about this issue, we find the Graphics.MeasureString Method, here is how to use it:
Graphics graphics = this.CreateGraphics(); SizeF textSize = graphics.MeasureString("How long am I?", this.Font);
Nice isn’t it? Well, there is one little problem here, how is the Graphics object created? The written code is a Windows Forms code, so the this is the Form itself. You can’t create the Graphics object by simply allocating it because it has no public constructor. The Control class contains a method for creating Graphics object, so you can create it once and use it whenever you need it later on. But, what if we are not using Windows Forms? Do we have to allocate some control just for that purpose? Fortunately, there is no need for that, we can use the TextRenderer::MeasureText Method:
Size textSize = TextRenderer.MeasureText("How long am I?", font);
The MeasureText is a static method of the TextRenderer class so it looks perfect. We can use it everywhere as a generic method for measuring text length in pixels. But can you see the problem with that method comparing to the MeasureString? Look at the returned object… The width and height returned are rounded down because the Size object is built from integers, not floats. This could lead to missing character on display unless you increase the size of the rectangle being measured a bit. In general, TextRenderer is less accurate because it uses GDI and Graphics uses GDI+.
I think that this is really a minor problem, but you can always use the first method, just don’t think that it is perfect, it has its own problems… What option would you use?
We pay for user submitted tutorials and articles that we publish. Anyone can send in a contribution
Learn More
PHenry Said on Oct 9, 2008 :
The only unknown with using the non Graphics way would be how printouts are handled. The Graphics context with the printer is going to give you different string lengths. Just something to be aware of (and requires testing :>). Great information.
asp.net Said on Oct 11, 2008 :
great review, thx
adrian(ro) Said on Jan 20, 2009 :
thx
Scuba_Steve Said on Mar 1, 2009 :
I am using TextRenderer.MeasureText to force word wrap on strings too long to fit in the containing div. The problem I am running into is that for font name Arial, it returns the same width of the string whether it is 12pt or 10pt and only one pixel less for 8pt on the same input string.
Scuba_Steve Said on Mar 1, 2009 :
Please let me know if you have any ideas why.
Shahar Y Said on Mar 1, 2009 :
@ Scuba_Steve
Try using Graphics.MeasureString Method instead,
it is more accurate.
Kluyg Said on Apr 14, 2009 :
TextRenderer is in Assembly System.Windows.Forms (in System.Windows.Forms.dll). So… “But, what if we are not using Windows Forms?”…
Need to create empty image, create graphics from that image (Graphics is in Assembly System.Drawing (in System.Drawing.dll)), measure string and then dispose all that shit… I think there is no better way. It’s sad.