Reputation: 10411
Here is some C# code written in linqpad to reproduce the issue.
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));
}
Anyone know the exact relationship between the string's length and the layout rectangle's width? The number 600581 seems very arbitrary. Although 65536 makes more sense as that is 0x10000.
Upvotes: 2
Views: 3023
Reputation:
Anyone know the exact relationship between the string's length and the layout rectangle's width? The number 600581 seems very arbitrary.
The number 600581 is indeed arbitrary and in this case it reflects the fonts char widths (or the typeface's glyphs).
Take for example 600581 / 65536
which gives an average char-width of 9.16 pixels per char which is reasonable for a fairly squared font such as Arial when you include spacing. GDI+ also adds padding to the text's bounding box in addition to this.
If you try a wider (or a narrower font, but you wouldn't notice unless reducing the number) as well as different letter combinations you should get different results. Try a mono-spaced font and you should be able to predict pretty accurately the needed boundaries (don't forget the padding).
If you really need to print long strings try to buffer the printing, however GDI+ isn't your best friend in these cases. Try the TextRenderer
class (a GDI wrapper) as already suggested:
http://msdn.microsoft.com/en-us/library/system.windows.forms.textrenderer.aspx
Upvotes: 2