TextRenderer.MeasureText 和 Graphics.MeasureString 大小不匹配
这不是舍入问题。差异 ~ 5+ 像素。
测试用例字符串:“”MACD(26,12,9)-0.000016”
e.Graphics.MeasureString("MACD (26,12,9) -0.000016", SystemFonts.DefaultFont).Width)
TextRenderer.MeasureText("MACD (26,12,9) -0.000016", SystemFonts.DefaultFont).Width)
结果总是:
139.3942
134
为什么大小差异如此之大?我只需要在paint方法之外对字符串宽度进行舍入。但它应该匹配 MeasureString 或反之亦然。
This is not a rounding problem. Difference ~ 5+ pixels.
Test Case String: ""MACD (26,12,9) -0.000016"
e.Graphics.MeasureString("MACD (26,12,9) -0.000016", SystemFonts.DefaultFont).Width)
TextRenderer.MeasureText("MACD (26,12,9) -0.000016", SystemFonts.DefaultFont).Width)
The result is always:
139.3942
134
Why is there so much difference in size? I just need the round of width of string outside paint method. But it should match MeasureString or vice versa.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
TextRenderer
使用 GDI 渲染文本,而Graphics
使用 GDI+。两者使用稍微不同的方法来布局文本,因此大小不同。您应该使用哪一种取决于最终用于实际绘制文本的内容。如果您使用 GDI+
Graphics.DrawString
进行绘制,请使用Graphics.MeasureString
进行测量。如果您使用 GDITextRenderer.DrawText
进行绘制,请使用TextRenderer.MeasureText
进行测量。如果文本将显示在 Windows 窗体控件内,则当
UseCompatibleTextRendering
设置为false
(这是默认值)时,它将使用TextRenderer
。阅读问题的字里行间,您似乎正在使用
TextRenderer
因为您在Paint
事件之外没有Graphics
实例。如果是这种情况,您可以自己创建一个来进行测量:如果您无权访问控件来创建图形实例,您可以使用它为屏幕创建一个实例,这对于测量目的来说效果很好。
TextRenderer
uses GDI to render the text, whereasGraphics
uses GDI+. The two use a slightly different method for laying out text so the sizes are different.Which one you should use depends on what will eventually be used to actually draw the text. If you are drawing it with GDI+
Graphics.DrawString
, measure usingGraphics.MeasureString
. If you are drawing using GDITextRenderer.DrawText
, measure usingTextRenderer.MeasureText
.If the text will be displayed inside a Windows Forms control, it uses
TextRenderer
ifUseCompatibleTextRendering
is set tofalse
(which is the default).Reading between the lines of your question, you seem to be using
TextRenderer
because you don't have aGraphics
instance outside thePaint
event. If that's the case, you can create one yourself to do the measuring:If you don't have access to a control to create the graphics instance you can use this to create one for the screen, which works fine for measurement purposes.
我创建了以下类来在绘制事件之外使用 MeasureString,并且效果非常好。
I Made the following class to use MeasureString outside the paint event, and it works pretty well.