佳能 EDSDK MemoryStream 图像
我已经和 Canon EDSDK 斗争了一段时间了。 我可以成功地让库将文件直接保存到磁盘,但是,我无法在内存中获取图像字节[]。 每当我尝试将 EDSDK 流 Marshal.Copy() 到 byte[] 时,我总是会收到以下错误:
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 版本中都看到了这种行为。
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.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
我刚刚在 google 上搜索了
EdsCreateMemoryStream
和 找到样本< /a> 其中还有另一个调用从“内存流”获取指针。然后,您可以使用
pointerToBytes
作为在Marshal.Copy
中读取的源。因此,我猜测您当前正在执行的操作是尝试从
stream
指向的一些小型控制结构的地址开始复制大量字节,因此您正在读取过去的内容该结构的末尾。编辑:顺便说一下,您的代码看起来好像有人告诉您应该只有一个 return 语句! 这是与 Fortran 和 C 等语言相关的旧建议; 这在现代语言中没有意义。 如果每次失败时立即返回错误代码,您的代码会更清晰(至少在这种情况下):(
更好的是,抛出一个包含错误代码和解释您要执行的操作的字符串的特定异常类。 )
I just googled for
EdsCreateMemoryStream
and found a sample in which there's another call to get the pointer from the "memory stream".You can then use
pointerToBytes
as the source to read from inMarshal.Copy
.So I'd guess that what you're currently doing is attempting to copy some large number of bytes starting from the address of some small control structure pointed to by
stream
, and hence you're reading past the end of that structure.Edit: By the way, you're code looks as if someone's told you that you should only have one return statement! That's old advice relating to languages like Fortran and C; it doesn't make sense in modern languages. Your code would be clearer (at least in this case) if you immediately returned the error code every time you got a failure:
(Better yet, throw a specific exception class containing the error code and a string explaining what you were trying to do.)
我意识到这是一篇旧文章,但这是一个完整的 C# 片段,用于从内存流下载。 它可能对其他人有用。 相机需要设置为 EDSDK.EdsSaveTo.Host 或 EDSDK.EdsSaveTo.Both
I realize that this is an old post, but this is a complete C# snippet for downloading from a memory stream. It may be useful for someone else. The camera needs to be set to EDSDK.EdsSaveTo.Host or EDSDK.EdsSaveTo.Both