Native Exception in AlphaImage Draw method


The problem is because a managed array is passed to the COM object. Once the garbage collector decides to free/move this array all hell break loose.
Here's a fix:
In IImagingFactory.cs, replace:
// We need the MarshalAs attribute here to keep COM interop from sending the buffer down as a Safe Array.
uint CreateImageFromBuffer([MarshalAs(UnmanagedType.LPArray)] byte[] buffer, uint size, BufferDisposalFlag disposalFlag, out IImage image);
uint CreateImageFromBuffer(IntPtr buffer, uint size, BufferDisposalFlag disposalFlag, out IImage image);
In AlphaImage.cs add this in the top of the file:
using System.Runtime.InteropServices;
And replace:
factory.CreateImageFromBuffer(pbBuf, cbBuf, BufferDisposalFlag.BufferDisposalFlagNone, out alphaImage._image);
// Copy stream data into an unmanaged global buffer that will be freed by the factory once image is decoded
// note that we cannot pass pbBuf directly since it might be moved around or freed by the gc.
IntPtr p = Marshal.AllocHGlobal((int)cbBuf);
Marshal.Copy(pbBuf, 0, p, (int)cbBuf);
factory.CreateImageFromBuffer(p, cbBuf, BufferDisposalFlag.BufferDisposalFlagGlobalFree, out alphaImage._image);


oliv26 wrote Nov 9, 2009 at 3:34 PM

Thanks a lot, I had the Exception and now it's OK!

wrote Nov 12, 2010 at 1:24 AM

INTPnerd wrote Nov 12, 2010 at 1:28 AM

Sweet bug fix! This really needs to be applied to the code base. The library completely unstable and unusable without this bug fix if your code uses the AlphaImage.CreateFromStream(...) or AlphaImage.CreateFromResource(...) methods at all.

wrote Feb 13, 2013 at 10:35 PM

liuyuhua wrote Sep 10, 2013 at 9:47 AM

I successfully solve the problem.