AlphaImage错误解决(zz)

AlphaImage中的这段代码出错 .... 不知道有谁知道怎样改??
public void Draw(Graphics gx, Rectangle bounds)
{
ImageInfo imgInfo;
_image.GetImageInfo(out imgInfo);

if (_alpha == 0)
{
// Draw the image, with alpha channel if any
IntPtr hdcDest = gx.GetHdc();
Rectangle dstRect = new Rectangle(bounds.X, bounds.Y, (int)imgInfo.Width + bounds.X, (int)imgInfo.Height + bounds.Y);
_image.Draw(hdcDest, ref dstRect, IntPtr.Zero);(这里产生异常...)
gx.ReleaseHdc(hdcDest);
}
else ....

异常信息:
位于 AlphaMobileControls.AlphaImage.Draw(Graphics gx, Rectangle bounds)
位于 AlphaMobileControls.AlphaPictureBox.Draw(Graphics gx)
位于 AlphaMobileControls.AlphaControl.DrawInternal(Graphics gx)
位于 AlphaMobileControls.AlphaContainer.OnPaint(PaintEventArgs e)
位于 AlphaMobileControls.AlphaForm.OnPaint(PaintEventArgs e)
位于 System.Windows.Forms.Control.WnProc(WM wm, Int32 wParam, Int32 lParam)
位于 System.Windows.Forms.ContainerControl.WnProc(WM wm, Int32 wParam, Int32 lParam)
位于 System.Windows.Forms.Form.WnProc(WM wm, Int32 wParam, Int32 lParam)
位于 System.Windows.Forms.Control._InternalWnProc(WM wm, Int32 wParam, Int32 lParam)
位于 Microsoft.AGL.Forms.EVL.EnterMainLoop(IntPtr hwnMain)
位于 System.Windows.Forms.Application.Run(Form fm)
位于 CMclient.Program.Main()
========================
{"未指明的错误 "} 未处理的COMException

  回复  引用  查看    
#13楼 2008-12-23 12:38 | suenihy      
http://www.codeplex.com/alphamobilecontrols/Thread/View.aspx?ThreadId=29775

是解决以上COM的错误的方法。

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);

With:

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);

With:

// 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);

你可能感兴趣的:(image,buffer,interop,stream,byte,file)