Get System.Drawing.Font width?

ε祈祈猫儿з 提交于 2019-12-07 06:27:12

问题


I'm using .Net tools to do some 2D drawing. System.Drawing.Font uses a GetHeight() that returns the height in pixels. I'm missing a GetWidth() to retrieve the width! What should I use?


回答1:


Use Graphics.MeasureString Method (String, Font):

Eg.

   // Set up string. string measureString = "Measure String";
    Font stringFont = new Font("Arial", 16);
    // Measure string.
    SizeF stringSize = new SizeF();
    stringSize = e.Graphics.MeasureString(measureString, stringFont);
    // This will give you string width, from which you can calculate further 
    double width = stringSize.Width



回答2:


What width? GetHeight returns the distance between the baselines of two lines of text, which is a property of the font itself. But the width depends on what you're going to write.

If you know what it is you want to write, try the Graphics.MeasureString methods.



来源:https://stackoverflow.com/questions/13269145/get-system-drawing-font-width

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!