gpt4 book ai didi

c# - MeasureString 奇怪的行为

转载 作者:行者123 更新时间:2023-11-30 14:34:54 29 4
gpt4 key购买 nike

如果我使用下面的文本宽度计算大小,它会给我说 500

intWidth = (int)objGraphics.MeasureString(String.concat(sImageText1, sImageText2), objFont).Width;

但如果我喜欢这样

intWidth = (int)objGraphics.MeasureString(sImageText1, objFont).Width + (int)objGraphics.MeasureString(sImageText2, objFont).Width;

我搜索了很多,也做过很多不同的场景,但如果我们使用“MeasureString”测量字符串宽度,全文或部分部分,仍然会有所不同。

这是为什么?

最佳答案

The documentation有答案:

The MeasureString method is designed for use with individual strings and includes a small amount of extra space before and after the string to allow for overhanging glyphs. Also, the DrawString method adjusts glyph points to optimize display quality and might display a string narrower than reported by MeasureString. To obtain metrics suitable for adjacent strings in layout (for example, when implementing formatted text), use the MeasureCharacterRanges method or one of the MeasureString methods that takes a StringFormat, and pass GenericTypographic. Also, ensure the TextRenderingHint for the Graphics is AntiAlias.

强调的部分是这里的相关内容。

要修复您的代码,请添加必要的参数:

intWidth = (int)objGraphics.MeasureString(sImageText1, objFont, 1024, Stringformat.GenericTypographic).Width
+ (int)objGraphics.MeasureString(sImageText2, objFont, 1024, StringFormat.GenericTypographic).Width;

关于c# - MeasureString 奇怪的行为,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13173263/

29 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com