"A generic error occurred in GDI+" while using MeasureString

I have an overriden (WinForms) MemoEdit control (unfortunately I can't give you the code). If I enter more than 32000 characters, it raises an error "A generic error occurred in GDI+" I thought that there's an error (exception) in my control but then I tested on a simple WinForm application this code:

var myString = new string('G', 32001);
var g = this.CreateGraphics();
g.MeasureString(myString, Font, 1000);

and it crashes with the same exactly error Does anyone know why this happens or where is specified this constant of 32000 ? I couldn't find anything useful on Google.

Upvotes: 6

Views: 3194

Answers (2)

Frank Hileman
Frank Hileman

Reputation: 1239

This seems to be a new bug which was introduced in Windows 8. I would be curious as to the behaviour in an older version of Windows:

https://connect.microsoft.com/VisualStudio/feedback/details/776722/label-control-throws-an-exception-if-its-text-is-set-to-a-very-long-string

Upvotes: 2

unknown6656
unknown6656

Reputation: 2963

It could be possible that the string width exceeds Int32.Max (you could maybe refer to this thread).

I know that your error occures if you use a language as Arabic which involves special characters (see MSDN-Link: For bidirectional languages, such as Arabic, the string length must not exceed 2046 characters).

To solve your problem I would suggest splitting the string every 31999th character and add the different sizes/lengths.

Upvotes: 0

Related Questions