.net2.0 vs .net 4.0 gdi+ difference?
I have a really weird problem where I have an application that does a lot of involved GDI+ manipulation of pictures. Such as cropping zooming etc. My application works fine in .net 2.0, but in .net 4.0 I am getting reports from my users that it is crashing with a gdi+ "out of memory" error. Now I know that the "out of memory" gdi+ error is a catch all for alot of errors, but why would it work in .net 2.0 and NOT on .net 4.
Specifically i have a control that draws "layers" on top of each other in order to create a composed bitmap. this control worked just fine in .net 2.0 and NOT in .net 4.
It secifically happens when I have a 10 megapixel jpeg loaded from the file system and I am applying a zoom and transform to the image.
to give even more detail. g.draw with a matrix scale of 4 meaning 400% bigger with any rotation will return this "out of memory error.
It only happens on xp boxes and NOT on windows 7 boxes. What could be the difference here?
here is the extent of my stack trace logged from the caught exception.
<Event> <TimeStamp>11/30/10 11:02:43.706</TimeStamp> <Source>APPro2</Source> <EventType>Error</EventType> <Message><![CDATA[##: OutOfMemoryException Message: Out of memory. Stack Trace: at System.Drawing.Graphics.CheckErrorStatus(Int32 status) at System.Drawing.Graphics.DrawImage(Image image, Rectangle destRect, Int32 srcX, Int32 srcY, Int32 srcWidth, Int32 srcHeight, GraphicsUnit srcUnit, ImageAttributes imageAttrs, DrawImageAbort callback, IntPtr callbackData) at System.Drawing.Graphics.DrawImage(Image image, Rectangle destRect, Int32 srcX, Int32 srcY, Int32 srcWidth, Int32 srcHeight, GraphicsUnit srcUnit, ImageAttributes imageAttr) at Colorvision.Graphics.Layers.Picture.DrawBig(Graphics g) in D:\Colorvision_Workspaces\Colorvision\Graphics\Layers\Picture.cs:line 321 at Colorvision.Graphics.LayerCollection.DrawBig(Graphics e) in D:\Colorvision_Workspaces\Colorvision\Graphics\LayerCollection.cs:line 690]]></Message> <EventID>0</EventID> </Event>
Thank you for your time. be gentle as this is my 1st question here.
0xa3 I have no stack trace at the moment, but the exact g.draw call is below:
g.DrawImage( bmpBigPicture, new Rectangle( destBigX, destBigY, (int)(destBigWidth*Scale), (int)(destBigHeight*Scale)), 0, 0, bmpBigPicture.Width, bmpBigPicture.Height, GraphicsUnit.Pixel, imgAttribs );
where scale 1s 4 for 400%
I faced similar issues. In my case the problem was LOH fragmentation. Maybe this will help: Large Object Heap Fragmentation
Basically you newer know for sure how memory is allocated. Sometimes you will get away with processing some large set of data, sometimes your app will fail. The problem is more likely to pop up if your program runs for long period of time and processes large amount of data. You mentioned 10 megapixels image - if you do a lot of processing with such files it's fairly easy to hit the LOH issue.
System.Drawing.Graphics.DrawImage directly calls a gdiplus function named "GdipDrawImageRectRectI". Whatever .NET Framework you use, there are not many versions of GDI+ and the way it's called is the same. Maybe this post can help: Latest version of GDI+ and support information
GDI+ may also be different if the system is X86 or X64. You want to make sure you're always comparing the same thing. Maybe you can track what GDI+ DLL is loaded in your process to help sort this out.