Users Online

We have 221 guests online
Measure Width of String in Pixels PDF Print E-mail
Written by Zack MIlls   
Monday, 21 December 2009 13:02

Measure Width of String in Pixels

 


public static int mGetStringLength(string sString, string sFont, float iFontSize)
{
    int iRet = 0;
    try
    {
        Font font = new Font(sFont, iFontSize);
        Bitmap bmp = new Bitmap(1, 1);
        Graphics g = Graphics.FromImage(bmp);
        int width = (int)g.MeasureString(sString, font).Width;
        iRet = width;
       g.Dispose();
    }
    catch { }
    return iRet;
}