这是一些用 linqpad 编写的 C# 代码,用于重现该问题。
var font = new System.Drawing.Font("Arial", 8);
using (var g = System.Drawing.Graphics.FromHwnd(IntPtr.Zero))
{
//65536 characters is fine
g.DrawString("a".PadLeft(65535, 'a'), font, System.Drawing.Brushes.Black, new System.Drawing.RectangleF(0, 0, 1, 1));
//65537 characters causes an error.
g.DrawString("a".PadLeft(65536, 'a'), font, System.Drawing.Brushes.Black, new System.Drawing.RectangleF(0, 0, 1, 1));
//65537 characters is however fine if the width is over 600581
g.DrawString("a".PadLeft(65536, 'a'), font, System.Drawing.Brushes.Black, new System.Drawing.RectangleF(0, 0, 600582, 1));
}
有人知道字符串的长度和布局矩形的宽度之间的确切关系吗?600581 这个数字似乎很随意。虽然 65536 更有意义,因为它是 0x10000。