GetChartBitmap: Generic GDI+ error
Author: Ben_Zurich
Creation Date: 3/19/2015 10:09 AM
profile picture

Ben_Zurich

#1
When collecting the bitmap of the chart

CODE:
Please log in to see this code.


sometimes we get the following error:

QUOTE:
************** Exception Text **************
System.Runtime.InteropServices.ExternalException (0x80004005): A generic error occurred in GDI+.
at System.Drawing.Graphics.Clear(Color color)
at WealthLab.ChartControl.Chart.OnPaint(PaintEventArgs pe)
at System.Windows.Forms.Control.PaintTransparentBackground(PaintEventArgs e, Rectangle rectangle, Region transparentRegion)
at System.Windows.Forms.Control.PaintTransparentBackground(PaintEventArgs e, Rectangle rectangle)
at System.Windows.Forms.Control.PaintBackground(PaintEventArgs e, Rectangle rectangle, Color backColor, Point scrollOffset)
at System.Windows.Forms.Control.PaintBackground(PaintEventArgs e, Rectangle rectangle)
at System.Windows.Forms.Control.OnPaintBackground(PaintEventArgs pevent)
at System.Windows.Forms.ScrollableControl.OnPaintBackground(PaintEventArgs e)
at System.Windows.Forms.Control.PaintWithErrorHandling(PaintEventArgs e, Int16 layer)
at System.Windows.Forms.Control.WmEraseBkgnd(Message& m)
at System.Windows.Forms.Control.WndProc(Message& m)
at System.Windows.Forms.NativeWindow.Callback(IntPtr hWnd, Int32 msg, IntPtr wparam, IntPtr lparam)


Apparently this has nothing to do with a particular chart of ours, as the error appears randomly.

Unfortunately, a Microsoft .NET Framework window pops up and requires a user response. Thus further processing is inhibited. Strangely enough, if I hit Continue in response of this window, no error is thrown that I could catch in my code.

Questions?
Is there a way to suppress such windows?
How would it be possible to catch such an error?
Is it possible that the cause for this error being that the chart is not yet ready when requested?
profile picture

Eugene

#2
This problem mostly occurs for security reasons i.e. not having enough permission to write to the destination folder.

Please provide the complete code and any other information required to consistently reproduce the error including settings related to charting.
profile picture

Ben_Zurich

#3
I'm not sure now whether it occurs in GetChartBitmap.

I rather think it occurs in

CODE:
Please log in to see this code.


and that this method has a callback to

CODE:
Please log in to see this code.


Is this correct? I think we should identify where this error comes from.


My Code around this:

CODE:
Please log in to see this code.



As we write all images into the same folder and all others are written to that location without problems, it cannot be a write access issue, isn't it?
profile picture

Eugene

#4
Guesswork is counterproductive. No one else seem to have had this problem before. I have no idea what your 'market' and 'general' and 'presentation' classes are doing behind the scenes, in which environment the code is executed, is it bug free, is it thread-safe. If insufficient permissions, as this is error is being commonly recognized, is not an issue, I can only conclude there is some error in your code.
profile picture

Ben_Zurich

#5
Sorry, don't forget it.
There are some other hints as to what could be the origin.

http://stackoverflow.com/questions/12002057/a-generic-error-occurred-in-gdi-webservice-resize-image-from-memorystream?rq=1

QUOTE:
"If for some reason an exception occurs in your function nothing will be disposed and files will stay open. Put the disposable objects in 'Using' blocks. – Jeroen Aug 17 '12 at 8:41

I second with what @Jeroen said, the problem may beacuse you are disposing the orginal image before saving the image. put this originalImage.Dispose(); as last line of code. – Mourya Aug 17 '12 at 12:52 "

Any thoughts on that?
profile picture

Ben_Zurich

#6
To put it simple, I do nothing else than:

CODE:
Please log in to see this code.


within the 'Execute()' method, and I very rarely get an unhandled exception. I don't think that this is a WL6 problem, nor my problem, but a windows issue, and it occurs randomly, and never at the same chart again.

The main issue here is that the exception reported in the first post is unhandled,, i.e. an exception is not thrown and instead a window pops up, so I cannot do anything about it.

I will further investigate that matter and, if you have any clue, appreciate any comments.




profile picture

Eugene

#7
As far as I can tell, GetChartBitmap puts the disposable object in a using block.

To not put it simple, your code did a lot of unknown things and this makes it our primary suspect. This innocuous code sample in post #6 is not enough for troubleshooting. We need as many information as possible to consistently reproduce the error - in the first place, your charting settings and workflow (i.e. how exactly you're using this code to get this exception), maybe even about your video driver.

For now, consider wrapping your code in a try/catch block.
profile picture

Ben_Zurich

#8
The Try/Catch block doesn't help because, as I said, no exception is thrown but a window pops up.

In the meantime, however, I was able to solve the problem:

In Visual C#, when a bitmap is assigned, depending on current usage of memory and other factors, the process of actually constructing the bitmap may be deferred, sometimes until the assigned bitmap is referenced for the first time.

In order to be able to still produce that bitmap at that later time, Visual C# blocks the original source of the bitmap, be it a file or a stream or whatever, because it still depends on that source in order to construct the bitmap.

In our case, some thread probably tried to access such a blocked source and thus caused that GDI+ error.

The workaround is to construct a second bitmap immediately after the GetChartBitmap() call in order to force Visual C# to actually construct the bitmap immediately after assigning it. The second step is then to set the first Bitmap to NULL in order to let Visual C# free its source.

The code to avoid the GDI+ error is as follows:

CODE:
Please log in to see this code.


With this trick we could avoid the GDI+ error and so far didn't have it again.

For a complete discussion of this issue see:

https://support.microsoft.com/en-us/kb/814675
profile picture

Eugene

#9
Thanks for sharing the workaround. In post #4, I suspected that the issue might have something to do with thread-safety or the like.
This website uses cookies to improve your experience. We'll assume you're ok with that, but you can opt-out if you wish (Read more).