佳能EDSDK MemoryStream图像

时间:2009-07-05 04:24:10

标签: c# dll unmanaged edsdk

我一直在与佳能EDSDK战斗一段时间。我可以成功地让库将文件直接保存到磁盘,但是,我无法在内存中保存图像byte []。每当我尝试将Marshal.Copy()EDSDK Stream转换为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中都看到了这种行为。

2 个答案:

答案 0 :(得分:4)

我只是用Google搜索了EdsCreateMemoryStreamfound a sample,其中还有另一个调用来从“内存流”中获取指针。

IntPtr pointerToBytes;
EDSDKLib.EDSDK.EdsGetPointer(stream, out pointerToBytes);

然后,您可以使用pointerToBytes作为来源Marshal.Copy

所以我猜你正在做的是尝试从stream指向的一些小控制结构的地址开始复制一些大量的字节,因此你正在阅读该结构的结束。

编辑:顺便说一句,你的代码看起来好像有人告诉你,你应该只有一个return语句!这是与Fortran和C等语言有关的旧建议;它在现代语言中没有意义。如果您在每次失败时立即返回错误代码,您的代码将更清晰(至少在这种情况下):

if ((err = EDSDK.EdsBlahBlah(...)) != EDSDK.EDS_ERR_OK)
    return err;

(更好的是,抛出一个包含错误代码的特定异常类和一个解释你想要做什么的字符串。)

答案 1 :(得分:3)

我意识到这是一篇旧帖子,但这是一个完整的C#片段,可以从内存流中下载。它可能对其他人有用。相机需要设置为EDSDK.EdsSaveTo.Host或EDSDK.EdsSaveTo.Both

        uint error = EDSDK.EDS_ERR_OK;
        IntPtr stream = IntPtr.Zero;

        EDSDK.EdsDirectoryItemInfo directoryItemInfo;

        error = EDSDK.EdsGetDirectoryItemInfo(this.DirectoryItem, out directoryItemInfo);

        //create a file stream to accept the image
        if (error == EDSDK.EDS_ERR_OK)
        {
            error = EDSDK.EdsCreateMemoryStream(directoryItemInfo.Size, out stream);
        }


        //down load image
        if (error == EDSDK.EDS_ERR_OK)
        {
            error = EDSDK.EdsDownload(this.DirectoryItem, directoryItemInfo.Size, stream);
        }

        //complete download
        if (error == EDSDK.EDS_ERR_OK)
        {
            error = EDSDK.EdsDownloadComplete(this.DirectoryItem);
        }


        //convert to memory stream
        IntPtr pointer; //pointer to image stream
        EDSDK.EdsGetPointer(stream, out pointer);

        uint length = 0;
        EDSDK.EdsGetLength(stream, out length);

        byte[] bytes = new byte[length];

        //Move from unmanaged to managed code.
        Marshal.Copy(pointer, bytes, 0, bytes.Length);

        System.IO.MemoryStream memoryStream = new System.IO.MemoryStream(bytes);
        Image image = System.Drawing.Image.FromStream(memoryStream);

        if (pointer != IntPtr.Zero)
        {
            EDSDK.EdsRelease(pointer);
            pointer = IntPtr.Zero;
        }


        if (this.DirectoryItem != IntPtr.Zero)
        {
            EDSDK.EdsRelease(this.DirectoryItem);
            this.DirectoryItem = IntPtr.Zero;
        }

        if (stream != IntPtr.Zero)
        {
            EDSDK.EdsRelease(stream);
            stream = IntPtr.Zero;
        }
相关问题