.net Graphics.MeasureString允许过多空白

.net Graphics.MeasureString允许过多空白,.net,gdi,.net,Gdi,我正在使用一个函数来调用在一个区域内呈现一段文本。 该功能的基本工作是: Dim measureSize as Size Do myFont = new Font(myFont.Name, myFont.Size - 1, FontStyle.Regular, GraphicsUnit.Document) 'Initial font size is set insanely high for the moment, during testing. 'Low initial

我正在使用一个函数来调用在一个区域内呈现一段文本。 该功能的基本工作是:

Dim measureSize as Size
Do
    myFont = new Font(myFont.Name, myFont.Size - 1, FontStyle.Regular, GraphicsUnit.Document)
    'Initial font size is set insanely high for the moment, during testing.
    'Low initial font size is not the problem.
    measureSize = g.MeasureString(myString, myFont)
Loop until (measuredSize.width < desiredSize.width AND measuredSize.height < desiredSize.height)
Dim测量尺寸大小
做
myFont=新字体(myFont.Name、myFont.Size-1、FontStyle.Regular、GraphicsUnit.Document)
“在测试过程中,初始字体大小目前设置得非常高。
'低初始字体大小不是问题。
measureSize=g.MeasureString(myString,myFont)
循环直到(measuredSize.width
问题是MeasureString在它所绘制的字符串周围添加了大量空白,并且最终的字体被渲染得太小

我肯定记得有人为了删除MeasureString方法中的所有填充,可以随意使用一些参数,但我的搜索目前没有显示任何内容


有人知道如何使用MeasureString来测量字符串的精确大小,而不使用任何边框吗?

您应该尝试使用
TextRenderer.MeasureText
,因为它返回文本的大小,而不是文本的大小(如果使用抗锯齿)


StringFormat.genericTyphographic
传递到
度量值
。我建议不要使用TextRenderer,因为它在渲染到内存位图时存在清除类型问题


使用TextRenderer进行测量和使用DrawString进行绘图无疑迟早会导致不一致。

请注意,要获得一致的结果,还需要将
StringFormat.GenericTypographic
传递给
DrawString
调用。看见