OutOfMemoryException when handling "large" pictures in NETCF
Nothing really unique in the blogosphere here, however worth mentioning anyway as it gives me the opportunity to discuss about MSDN Forums. Recently I've been handling yet another case about a NETCF Memory Leak... I have to admit that after my initial post about that topic the incoming requests about memory leaks are kind of dramatically decreased... In any case this time it was yet something well-known in NETCF, about the peculiar way it handles Drawing-associated resources.
So, imagine you want to programmatically handle a "very large" picture through a System.Drawing.Bitmap object. In NETCF, a "very large" picture is for example one with 1, 2 or 3 MB in size. Using System.Drawing.Bitmap for so large pictures is NOT practical in a NET Compact Framework application, because you don't have so much memory to waste in the GC Heap. Consider that the usual amount of data within the Managed Heap is 1 or 2 MB, in some rare cases it can reach 3 or 4 MB, but no more.
So, the memory to be used can't be MANAGED, it must be NATIVE. How to use native memory to load the image? The solution came from MSDN Forums, which is an incredibly valuable place to look for common solutions and suggestions. It's so useful that threads are even indexed by MSDN Search. Visit the Microsoft Technical Communities home page, to look at all the resources available for your community!
In the specific case, the solution consisted on using COM Interoperability to invoke COM interfaces like IImagingFactory (https://msdn.microsoft.com/en-us/library/aa918428.aspx) that interacts with the native resources that are underlying a bitmap. The MSDN Forum thread was https://forums.microsoft.com/MSDN/ShowPost.aspx?PostID=2181275&SiteID=1.