保存图像到MemoryStream-通用GDI +错误 [英] Saving Image to MemoryStream- Generic GDI+ Error

查看:253
本文介绍了保存图像到MemoryStream-通用GDI +错误的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的应用程序概述:在客户端,一系列快照采取一个摄像头。在提交,我想图像转换为字节数组,并送至我写了一个服务,它字节数组。

Overview of my application: On the client side, a series of snapshots are taken with a webcam. On submit, I want the images to be converted to a byte array, and have that byte array sent to a service I have written.

我的问题:我想要一个单一的图像保存到的MemoryStream ,但它继续突破,吐出消息, GDI +中发生一般性错误。:当我深入,我看到,当MemoryStream的缓冲区位置在54不幸的是,抛出异常,这是一个 1.2 MB 照片。这里是code块:

My problem: I'm trying to save a single image to a MemoryStream, but it continues to break, spitting out the message, "A generic error occured in GDI+." When I dig deeper, I see that the exception is thrown when the MemoryStream's buffer position is at 54. Unfortunately, it's a 1.2 mb photo. Here's the block of code:

// Create array of MemoryStreams
var imageStreams = new MemoryStream[SelectedImages.Count];
for (int i = 0; i < this.SelectedImages.Count; i++)
{   
    System.Drawing.Image image = BitmapFromSource(this.SelectedImages[i]);
    imageStreams[i] = new MemoryStream();
    image.Save(imageStreams[i], ImageFormat.Bmp); /* Error is thrown here! */
}

// Combine MemoryStreams into a single byte array (Threw this 
// in in case somebody has a better approach)
byte[] bytes = new byte[imageStreams.Sum(s => s.Length)];
for(int i = 0; i < imageStreams.Length; i++)
{
    bytes.Concat(imageStreams[i].ToArray());
}

这是我的BitmapFromSource方法

And here is my BitmapFromSource method

// Converts a BitmapSource object to a Bitmap object
private System.Drawing.Image BitmapFromSource(BitmapSource source)
{
    System.Drawing.Image bitmap;

    using (MemoryStream ms = new MemoryStream())
    {
        BitmapEncoder encoder = new BmpBitmapEncoder();
        encoder.Frames.Add(BitmapFrame.Create(source));
        encoder.Save(ms);
        bitmap = new System.Drawing.Bitmap(ms);
    }
    return bitmap;
}

很多东西我看了一下通用GDI +错误指向一个权限问题,但我看不出这将适用于此处,考虑到我没有保存到文件系统。另外,我已经看到了这个错误可能出现由于收盘的MemoryStream在保存图像之前,但我也看不出如何做到这一点适用考虑到我立即创建的MemoryStream之前,我保存图像。任何有识之士将大大AP preciated。

A lot of what I have read about the Generic GDI+ Error points to a permissions issue, but I don't see how that would apply here, considering I'm not saving to the file system. Also, I've seen that this error can arise due to the MemoryStream closing before the image is being saved, but I also don't see how this would apply considering I create the MemoryStream immediately before I save the image. Any insight would be greatly appreciated.

推荐答案

我觉得你的问题实际上在于你BitmapFromSource方法。

I think your problem actually lies in your BitmapFromSource method.

您正在创建一个流,然后创建从该流的位图,然后扔流走,然后试图将位图保存到另一个流。然而,Bitmap类的文件说:

You're creating a stream, then creating a bitmap from that stream, then throwing the stream away, then trying to save the bitmap to another stream. However, the documentation for the Bitmap class says:

您必须保持开放流的位图的寿命。

You must keep the stream open for the lifetime of the Bitmap.

由你来保存位图时,位图已经被损坏,因为你放弃原来的流了。

By the time you come to save that bitmap, the bitmap is already corrupted because you've thrown the original stream away.

请参阅:<一href=\"http://msdn.microsoft.com/en-us/library/z7ha67kw\">http://msdn.microsoft.com/en-us/library/z7ha67kw

要解决这个问题(铭记我没有写的code更不用说测试了它),在你的code的第一个块创建第一个循环内的MemoryStream,而内存流传递到您BitmapFromSource方法作为第二个参数。

To fix this (bearing in mind I've not written the code let alone tested it), create the MemoryStream inside the first for loop in your first block of code, and pass that memory stream to your BitmapFromSource method as a second parameter.

这篇关于保存图像到MemoryStream-通用GDI +错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆