C# 在不使用 Graphics 对象的情况下测量字符串?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/1003370/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Measure a String without using a Graphics object?
提问by Agnel Kurian
I am using pixels as the unit for my font. In one place, I am performing a hit test to check if the user has clicked within the bounding rectangle of some text on screen. I need to use something like MeasureStringfor this. Unfortunately, the code doing the hit test is deep within a library which does not have access to a Graphicsobject or even a Control.
我使用像素作为我的字体单位。在一个地方,我正在执行命中测试以检查用户是否在屏幕上某些文本的边界矩形内进行了单击。我需要为此使用类似的东西MeasureString。不幸的是,执行命中测试的代码位于库的深处,该库无法访问Graphics对象甚至Control.
How do I get the bounding box of a string given the font without using the Graphicsclass? Why do I even need a Graphicsobject when my font is in pixels?
如何在不使用Graphics类的情况下获取给定字体的字符串的边界框?Graphics当我的字体以像素为单位时,为什么我什至需要一个对象?
采纳答案by Jarrod
回答by NerdFury
You don't need to use the graphics object that you are using to render to do the measuring. You could create a static utility class:
您不需要使用用于渲染的图形对象来进行测量。您可以创建一个静态实用程序类:
public static class GraphicsHelper
{
public static SizeF MeasureString(string s, Font font)
{
SizeF result;
using (var image = new Bitmap(1, 1))
{
using (var g = Graphics.FromImage(image))
{
result = g.MeasureString(s, font);
}
}
return result;
}
}
It might be worthwile, depending on your situation to set the dpi of the bitmap as well.
根据您的情况设置位图的 dpi 可能是值得的。
回答by Anton Putov
MeasureStringmethod in @NerdFury answer will give a higher string width than expected.Additional info you can find here. If you want to measure only the physical length please add this two lines :
MeasureString@NerdFury 答案中的方法将提供比预期更高的字符串宽度。您可以在此处找到其他 信息。如果您只想测量物理长度,请添加以下两行:
g.TextRenderingHint = System.Drawing.Text.TextRenderingHint.AntiAlias;
result =
g.MeasureString(measuredString, font, int.MaxValue, StringFormat.GenericTypographic);
回答by Jacob Raines
This example does great job of illustrating the use of FormattedText. FormattedText provides low-level control for drawing text in Windows Presentation Foundation (WPF) applications. You can use it to measure the Width of a string with a particular Font without using a Graphics object.
这个例子很好地说明了 FormattedText 的使用。FormattedText 为在 Windows Presentation Foundation (WPF) 应用程序中绘制文本提供低级控制。您可以使用它来测量具有特定字体的字符串的宽度,而无需使用 Graphics 对象。
public static float Measure(string text, string fontFamily, float emSize)
{
FormattedText formatted = new FormattedText(
item,
CultureInfo.CurrentCulture,
System.Windows.FlowDirection.LeftToRight,
new Typeface(fontFamily),
emSize,
Brushes.Black);
return formatted.Width;
}
Include WindowsBase and PresentationCore libraries.
包括 WindowsBase 和 PresentationCore 库。
回答by Peter H?gstedt
This may not be but a repeat of others, but my problem was the unwanted Graphics object as well. After listening to the above frustrations I simply tried:
这可能只是其他人的重复,但我的问题也是不需要的 Graphics 对象。听完上面的挫败感后,我只是尝试了:
Size proposedSize = new Size(int.MaxValue, int.MaxValue);
TextFormatFlags flags = TextFormatFlags.NoPadding;
Size ressize = TextRenderer.MeasureText(content, cardfont, proposedSize, flags);
(where 'content' is the string to measure and cardfont the font it is in)
(其中 'content' 是要测量的字符串,cardfont 是它所在的字体)
... and was in luck. I could use the result to set the width of my column in VSTO.
...而且很幸运。我可以使用结果来设置 VSTO 中列的宽度。

