首先,如果是asp.net 网站,先添加对System.Windows.Forms 的引用。在asp.net中添加对他的引用似乎不伦不类,管他呢,反正能解决问题
这是代码:
//定义一个control,需要用到它的一个方法CreateGraphics()来生成一个Graphics对象
//然后调用Graphics对象的MeasureString方法,传入字体名称,大小,样式(比如bold)等,去他的width属性即可得到传入字符串的宽度
System.Windows.Forms.Control c = new System.Windows.Forms.Control();
float width = c.CreateGraphics().MeasureString("这是需要获得宽度的字符串this is my string", new System.Drawing.Font("Georgia", 36, System.Drawing.FontStyle.Bold)).Width;