I've been fighting with the Canon EDSDK for a while now. I can successfully get the library to save a file directly to disk, however, I cannot get a hold of the image byte[] in memory. Whenever I attempt to Marshal.Copy() the EDSDK Stream to byte[], I always get the following error:
AccessViolationException: Attempted to read or write protected memory. This is often an indication that other memory is corrupt.
Below is one of the variations of code that I've used to try and get the stream:
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;
}
Breakpoints reveal (Through the EdsDirectoryItemInfo object) that the image is indeed there, I just don't know why I'd be getting the exception that I am. I have been toying with the idea of accepting defeat and just read the resultant image from disk that it so readily writes via the CreateFileStream method, but I really ought to just be able to manipulate the image in memory.
Any ideas?
UPDATE: I see this behavior in both versions 2.5 and 2.6.