我一直在與佳能EDSDK戰鬥了一段時間。我可以成功地讓庫將文件直接保存到磁盤,但是,我無法在內存中保留圖像byte []。每當我試圖Marshal.Copy()的EDSDK流爲byte [],我總是得到以下錯誤:佳能EDSDK MemoryStream圖片
AccessViolationException:嘗試讀取或寫入受保護的內存。這通常表明其他內存已損壞。
下面是我用來嘗試並獲得流的編碼的變化之一:
private uint downloadImage(IntPtr directoryItem)
{
uint err = EDSDK.EDS_ERR_OK;
IntPtr stream = IntPtr.Zero;
// Get information of the directory item.
EDSDK.EdsDirectoryItemInfo dirItemInfo;
err = EDSDK.EdsGetDirectoryItemInfo(directoryItem, out dirItemInfo);
// Create a file stream for receiving image.
if (err == EDSDK.EDS_ERR_OK)
{
err = EDSDK.EdsCreateMemoryStream(dirItemInfo.Size, out stream);
}
// Fill the stream with the resulting image
if (err == EDSDK.EDS_ERR_OK)
{
err = EDSDK.EdsDownload(directoryItem, dirItemInfo.Size, stream);
}
// Copy the stream to a byte[] and
if (err == EDSDK.EDS_ERR_OK)
{
byte[] buffer = new byte[dirItemInfo.Size];
GCHandle gcHandle = GCHandle.Alloc(buffer, GCHandleType.Pinned);
// The following line is where it blows up...
Marshal.Copy(stream, buffer, 0, (int)dirItemInfo.Size);
// ... Image manipulation, show user, whatever
}
return err;
}
斷點顯示(通過EdsDirectoryItemInfo對象)的圖像確實是存在的,我只是不不知道爲什麼我會得到我的例外。我一直在接受失敗的想法,只是從磁盤讀取結果圖像,它很容易通過CreateFileStream方法寫入,但我真的應該只能夠操縱內存中的圖像。
任何想法?
更新:我在版本2.5和2.6中看到此行爲。
你介意張貼代碼(的地方),下載您只是把磁盤的圖片?我甚至無法達到這個目標。具體來說,我不能讓它調用我的對象事件處理函數。 – andrewrk 2009-07-29 01:05:58