Graphics.MeasureString返回不同的值的Win32 GetTextExtent [英] Graphics.MeasureString returns different values than Win32 GetTextExtent

查看:551
本文介绍了Graphics.MeasureString返回不同的值的Win32 GetTextExtent的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我一直在试图寻找在C#中的方法来衡量一个字符串的大小。该标准方法来衡量一个字符串在Win32中是使用GetTextExtent。真正的目标是要找到的平均值的字体字符的宽度和高度。标准的方法来找到一个字符的平均宽度开始从52得到的所有字母和鸿沟的宽度:

i've been trying to find a method in C# to measure the size of a string. The standard method to measure a string in Win32 is to use GetTextExtent. The real goal is to find the average width and height of a font character. The standard method to find the average width of a character starts from getting the width of all alphabetic characters and divide by 52:

size = dc.GetTextExtent(
       "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz", 52);
averageWidth = size / 52;

微软有一个网页,列出了平均字符宽度的一些字体大小在一定的DPI设置,我已经通过我自己的调用GetTextExtent证实了他们。

Microsoft has a page that lists the average character widths for some font sizes at certain DPI settings, and i've confirmed them by my own calls to GetTextExtent.

  • 宋体8PT,96DPI: 13x6 PX
  • 宋体宋体,96DPI: 14x7 PX
  • 的Segoe UI宋体,96DPI :15x7 PX
  • Tahoma 8pt, 96dpi: 13x6 px
  • Tahoma 9pt, 96dpi: 14x7 px
  • Segoe UI 9pt, 96dpi: 15x7 px

现在我想执行在.NET的WinForms同样的计算。使用Graphics.MeasureString()我拿出code:

Now i want to perform the same calculations under .NET WinForms. Using Graphics.MeasureString() i come up with code:

public static SizeF GetAvgCharSizeF(Graphics g, Font font)
{
   String s = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz";

   SizeF textSize = g.MeasureString(s, font);

   float baseUnitX = (textSize.Width / s.Length);
   float baseUnitY = textSize.Height;

   return new SizeF(baseUnitX, baseUnitY);
}

不幸的值不匹配已知的,公认的,真正的价值:

Unfortunatly the values do not match the known, accepted, true values:

  • 宋体8PT,96DPI:一个14x6像素的(14.21x6.09 PX)
  • 宋体宋体,96DPI: 16x7像素的(15.98x6.85 PX)
  • 的Segoe UI宋体,96DPI: 17x7像素的(17.46x6.91 PX)
  • Tahoma 8pt, 96dpi: 14x6 px (14.21x6.09 px)
  • Tahoma 9pt, 96dpi: 16x7 px (15.98x6.85 px)
  • Segoe UI 9pt, 96dpi: 17x7 px (17.46x6.91 px)

平均字符宽度变成确定,但字符高度为约13%,过大。假设我的额外高度是由于在高度的分类的差已被更改为包括伸部和下伸。如果测量线被测量太高,我试图改变它:

The average character widths turn out OK, but the character heights are too large by about 13%. i assume that the extra height is due to a difference in the classification of height having been changed to include ascenders and descenders. If the measuring string was measuring too tall, i tried changing it from:

String s = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz";

String s = "acemnorsuvwxyz";

思维,现在的字符串不一样高,所以测得的高度要短些。不,它返回的确切的相同高度的字符串,其中包括上行字母和下行(虽然平均宽度稍大),版本

thinking that now the string is not as tall, and so the measured Height should be shorter. Nope, it returns the exact same heights as the version of the string that included ascenders and descenders (although the average widths are slightly larger).

有什么可以这样做的原因,并能得到平均字符高度可以做些什么来的平均文字高度,使其匹配返回GetTextExtent接受的价值观?

What can be the cause of this, and can get the average character height what can be done to the average text height so that it matches the accepted values returned by GetTextExtent?

注:虽然标准的做法,以获得平均字符高度为使用GetTextMetrics,通过GetTextExtents返回的高度返回相同的值

Note: Even though the standard practice to get the average character height is to use GetTextMetrics, the height returned by GetTextExtents returns the same value.

推荐答案

我怀疑这是因为文本渲染引擎是.NET不同。您可能要查找GDI和GDI +之间的差异。事情也可能会再次为你改变,如果你调用 Application.SetCompatibleTextRenderingDefault(小于布尔>)法,用true或false作为参数(我相信默认为真)

I suspect this is because the text rendering engine is different in .NET. You might want to lookup the differences between GDI and GDI+. Things also might change again for you if you call the Application.SetCompatibleTextRenderingDefault(<bool>) method, with true or false as the parameter (I believe the default is true).

在一般情况下,如果你正在使用Windows API绘制,然后进行测量与Windows API,如果你正在使用.NET绘图,测量与.NET。

In general, if you're drawing with the Windows API, then measure with the Windows API, and if you're drawing with .NET, measure with .NET.

这篇关于Graphics.MeasureString返回不同的值的Win32 GetTextExtent的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆