简体   繁体   English

如何在屏幕上获取字符串的宽度和高度?

[英]How to get width and height of string on the screen?

Lets say I've a string and some font (name, size, etc...). 可以说我有一个字符串和一些字体(名称,大小等)。
How to get width and height, measured in pixels, of such string displayed on the screen? 如何获得在屏幕上显示的这种字符串的宽度和高度(以像素为单位)?

The easy answer is Graphics.MeasureString. 简单的答案是Graphics.MeasureString。 But it's rather infamous for not being particularly accurate. 但是,由于不够准确而臭名昭著。 If you need better accuracy you can check out this article: Using MeasureCharacterRanges to Draw Text . 如果需要更高的准确性,可以查看本文: 使用MeasureCharacterRanges绘制文本

You didn't specify if you were using WinForms or ASP.NET. 您没有指定使用的是WinForms还是ASP.NET。

If this is a web site, the answer I gave on this question can be easily distilled to a working solution for your question: 如果这是一个网站,那么我对这个问题的回答可以很容易地提炼成针对您问题的可行解决方案:

third font in font family is significantly larger 字体家族中的第三种字体明显更大

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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