佳能 EDSDK MemoryStream 图像

发布于 2024-07-26 08:01:14 字数 1692 浏览 1 评论 0原文

我已经和 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

太阳男子 2024-08-02 08:01:14

我刚刚在 google 上搜索了 EdsCreateMemoryStream找到样本< /a> 其中还有另一个调用从“内存流”获取指针。

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;

更好的是,抛出一个包含错误代码和解释您要执行的操作的字符串的特定异常类。 )

I just googled for EdsCreateMemoryStream and found a sample in which there's another call to get the pointer from the "memory stream".

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

You can then use pointerToBytes as the source to read from in Marshal.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:

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

(Better yet, throw a specific exception class containing the error code and a string explaining what you were trying to do.)

凹づ凸ル 2024-08-02 08:01:14

我意识到这是一篇旧文章,但这是一个完整的 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;
        }

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

        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;
        }
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文