我試圖使用字節數組使用下面的代碼動態創建一個位圖圖像位圖創作C#:使用字節數組
Bitmap GetImage()
{
IntPtr ip = Marshal.AllocCoTaskMem(imagesize);
//some code to fill ip
Image img = new Bitmap(
w,
h,
-stride,
PixelFormat.Format24bppRgb,
(IntPtr)(ip.ToInt32() + imagesize - stride)
);
Marshal.FreeCoTaskMem(ip); // Comment this line to work
return img;
}
void SaveImage()
{
Image img = GetImage();
img.save("test.bmp"); // This call fails
img.Dispose();
}
現在我的問題是我想回到一個位圖,但在同一時間我不t想要保持ip指針的內存分配泄漏。它永遠不會被釋放。 如何返回使用字節數組創建的圖像,並且不會打擾函數GetImage()的調用方來釋放內存。
我正在從非託管緩衝區,只不過是DirectX的SampleGrabber過濾器中的數據,這是我現在用AllocCoTaskMem – Ankit 2009-05-03 20:26:44