简体   繁体   中英

server side display string width

How can I calculate the display width size of a string based on a font family (arial) and size in pixels (12px)? (if necessary, assume zoom level 100%)

I have seem a few posts about it, but I still cant figure it out.

Try using the TextRenderer class - see MSDN documentation here: http://msdn.microsoft.com/en-us/library/69ye7hc8.aspx

Here is an example Console application that measures the string "This is some text":

using System;
using System.Windows.Forms;
using System.Drawing;

namespace TextMeasureExample
{
    class Program
    {
        static void Main(string[] args)
        {
            Console.WriteLine(TextRenderer.MeasureText("This is some text", new Font("Arial", 0.75f)));
        }
    }
}

That can be tough to do accurately. But this should point you in the right direction: how to use System.Drawing.Graphics.MeasureString?

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM