我正在尝试使用TextRenderer类测量给定某种字体的字符串的大小。尽管我尝试使用3种不同的方法(Graphics.MeasureCharacterRanges,Graphics.MeasureString,TextRenderer.MeasureText)测量它,但它们都给出了不同的结果而且不准确,我偶然发现了其他的东西。
使用7和8的字体大小使用相同的字体测量相同的字符串START
,fontsize 7测量结果比fontsize 8测量更宽。
这是我使用的代码:
Font f1 = new Font("Arial", 7, FontStyle.Regular);
Font f2 = new Font("Arial", 8, FontStyle.Regular);
Size s1 = TextRenderer.MeasureText("START", f1);
Size s2 = TextRenderer.MeasureText("START", f2);
结果是s1
width
为41,height
为13,而s2
的{{1}}为40且width
14。
为什么较小的字体会产生较大的宽度?
答案 0 :(得分:2)
为了明确地解决为什么,更大的字体可以产生更小的宽度,我将这个示例控制台应用程序放在一起。值得注意的是,我调整了7& 8种字体大小到7.5&分别为8.25,因为这是TextRenderer
在内部评估它们的大小。
using System;
using System.Drawing;
using System.Linq;
using System.Runtime.InteropServices;
namespace FontSizeDifference
{
static class Program
{
[StructLayout(LayoutKind.Sequential)]
struct ABCFLOAT
{
public float abcfA;
public float abcfB;
public float abcfC;
}
[DllImport("gdi32.dll")]
static extern bool GetCharABCWidthsFloat(IntPtr hdc, int iFirstChar, int iLastChar, [Out] ABCFLOAT[] lpABCF);
[DllImport("gdi32.dll", CharSet = CharSet.Auto, EntryPoint = "SelectObject", SetLastError = true)]
static extern IntPtr SelectObject(IntPtr hdc, IntPtr obj);
[DllImport("gdi32.dll", EntryPoint = "DeleteObject")]
static extern bool DeleteObject([In] IntPtr hObject);
[StructLayout(LayoutKind.Sequential)]
struct KERNINGPAIR
{
public ushort wFirst;
public ushort wSecond;
public int iKernAmount;
}
[DllImport("gdi32.dll")]
static extern int GetKerningPairs(IntPtr hdc, int nNumPairs, [Out] KERNINGPAIR[] lpkrnpair);
[STAThread]
static void Main()
{
var fonts = new[] {
new Font("Arial", 7.5f, FontStyle.Regular),
new Font("Arial", 8.25f, FontStyle.Regular)
};
string textToMeasure = "START";
using (Graphics g = Graphics.FromHwnd(IntPtr.Zero))
{
IntPtr hDC = g.GetHdc();
foreach (Font font in fonts)
{
float totalWidth = 0F;
IntPtr hFont = font.ToHfont();
// Apply the font to dc
SelectObject(hDC, hFont);
int pairCount = GetKerningPairs(hDC, short.MaxValue, null);
var lpkrnpair = new KERNINGPAIR[pairCount];
GetKerningPairs(hDC, pairCount, lpkrnpair);
Console.WriteLine("\r\n" + font.ToString());
for (int ubound = textToMeasure.Length - 1, i = 0; i <= ubound; ++i)
{
char c = textToMeasure[i];
ABCFLOAT characterWidths = GetCharacterWidths(hDC, c);
float charWidth = (characterWidths.abcfA + characterWidths.abcfB + characterWidths.abcfC);
totalWidth += charWidth;
int kerning = 0;
if (i < ubound)
{
kerning = GetKerningBetweenCharacters(lpkrnpair, c, textToMeasure[i + 1]).iKernAmount;
totalWidth += kerning;
}
Console.WriteLine(c + ": " + (charWidth + kerning) + " (" + charWidth + " + " + kerning + ")");
}
Console.WriteLine("Total width: " + totalWidth);
DeleteObject(hFont);
}
g.ReleaseHdc(hDC);
}
}
static KERNINGPAIR GetKerningBetweenCharacters(KERNINGPAIR[] lpkrnpair, char first, char second)
{
return lpkrnpair.Where(x => (x.wFirst == first) && (x.wSecond == second)).FirstOrDefault();
}
static ABCFLOAT GetCharacterWidths(IntPtr hDC, char character)
{
ABCFLOAT[] values = new ABCFLOAT[1];
GetCharABCWidthsFloat(hDC, character, character, values);
return values[0];
}
}
}
对于每种字体大小,它输出每个字符的宽度,包括字距调整。在96 DPI,对我来说,这导致:
[字体:名称= Arial,尺寸= 7.5,单位= 3,GdiCharSet = 1,GdiVerticalFont =假]
S:7(7 + 0)
T:6(7 + -1)
答:7(7 + 0)
R:7(7 + 0)
T:7(7 + 0)
总宽度:34[字体:名称= Arial,尺寸= 8.25,单位= 3,GdiCharSet = 1,GdiVerticalFont =假]
S:7(7 + 0)
T:5(6 + -1)
答:8(8 + 0)
R:7(7 + 0)
T:6(6 + 0)
总宽度:33
虽然我显然没有捕捉到TextRenderer
所做测量的确切公式,但它确实说明了相同的宽度差异。在字体大小为7时,所有字符的宽度均为7。但是,在字体大小为8时,字符宽度开始变化,有些较大,有些较小,最终加起来的宽度较小。
答案 1 :(得分:1)