Graphics.MeasureString allowing too much whitespace

血红的双手。 提交于 2021-02-07 04:15:18

问题


I'm using a function to call for a piece of text to be rendered within an area. The basic working of the function is:

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)

The problem with this is that MeasureString is adding a lot of whitespace around the string it is drawing, and the final font is being rendered far too small.

I'm sure I remember some argument one can fiddle with in order to remove all the padding from the MeasureString method, but my searches currently aren't turning up anything.

Does anyone know how to use MeasureString to measure the EXACT size of the string without any bordering at all?


回答1:


Pass StringFormat.GenericTypographic to MeasureString. I recommend against TextRenderer because it has problems with clear-type when rendering to an in-memory bitmap.

Using TextRenderer for measurement and DrawString for drawing will undoubtedly get you into an inconsistency sooner or later.




回答2:


You should have a try with TextRenderer.MeasureText, as it returns the size of the text rather than the size of the text if anti-aliased.

MSDN Article



来源:https://stackoverflow.com/questions/798336/graphics-measurestring-allowing-too-much-whitespace

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!