Text size is a bit off using Graphics and very wrong using GraphicsPath
#7,485 创建于 2022年7月23日
仓库指标
- Star
- (4,100 star)
- PR 合并指标
- (平均合并 14天 22小时) (30 天内合并 56 个 PR)
描述
.NET version
.NET Framework 4.7.2
Did it work in .NET Framework?
No
Did it work in any of the earlier releases of .NET Core or .NET 5+?
probably in none
Issue description
What was my initial goal:
- create a rectangle which by resizing, fits text into itself
- this rectangle should show the bounds of the text and the text should fit tightly into the rectangle (just like the blue
GraphicsPathsample does but its size is wrong)
This seems like an impossible task with current WinForms API because there are two string size calculations which produce different results and both are wrong. Graphics is approx. to 90 % right while GraphicsPath is like 60 % right or less,
Drawing a string with font size 60 points on coordinates [200, 200] with Graphics and GraphicsPath produces two very different results (shown by text).
String size calculation using Graphics.MeasureString and GraphicsPath.GetBounds methods produces two very different results (shown by rectangles).
- bounding rectangle of the Graphics is OK with the position
- bounding rectangle of the GraphicsPath is sligtly shifted compared to the position (and I am not really able to calculate why and how)
- both Graphics and GraphicsPath should have top left corner at [200, 200]
- a bit about this is explained here https://stackoverflow.com/questions/51006343/winforms-drawing-a-path-in-the-right-place

Steps to reproduce
Note: because of the GraphicsPath behavior, the best size calculation (fit into rectangle) can be done by bruteforcing font size from largest to lowest using GraphicsPath with GetBounds. However the result works fine only for drawing by GraphicsPath (if drawn by Graphics with font size calculated using GraphicsPath, the text will be much bigger because font sizes returned by GraphicsPath are very small (unrealistic))
- what
GraphicsPathonly does well is calculating the bounding rectangle of the string (wrong font size and shifted position!)
Edit: this project shows how to get the drawn position of GraphicsPath right, however it is kinda complicated because if GraphicsPath contains multiple objects, this must probably be done for every object (create an additional GraphicsPath object, measure size and offset, drop the GraphicsPath object and then use the correction to draw the actual GraphicsPath object):
GraphicsPath corrected drawing location.zip
- the font size compared to Graphics.MeasureString issue persists

