Home
Manage Your Code
Snippet: MeasureString (C#)
Title: MeasureString Language: C#
Description: How to measure the rendered height of a string of text at a given font and width constraint Views: 809
Author: Scott Reynolds Date Added: 3/20/2008
Copy Code  
1private int MeasureStringHeight(int width, string textToMeasure, Font font)
2{
3    Bitmap bmp = new Bitmap(1,1);
4    Graphics g = Graphics.FromImage(bmp);
5    SizeF bounds = g.MeasureString(textToMeasure, font, width);
6    int height = Convert.ToInt32(bounds.Height);
7    //This part is to convert pixels to inches, which is what I needed.

8    //You could just return height for pixels

9    return height/g.DpiY;
10}