GDI +的异常保存位图到一个MemoryStream [英] GDI+ exception saving a Bitmap to a MemoryStream

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

问题描述

我有一个Windows中的一个问题窗体应用程序与Bitmap.Save失败时,我保存到一个MemoryStream。这个问题似乎只发生间歇性地在一台机器(目前为止),坏消息是在客户现场。我无法调试的机器上,但是我得到的将问题范围缩小到了code一行堆栈跟踪。

I have a problem in a Windows Forms application with Bitmap.Save failing when I save to a MemoryStream. The problem only seems to occur intermittently on one machine (so far) and the bad news it is at a customer site. I can't debug on the machine, but I got a stack trace that narrowed the problem down to a single line of code.

下面是我的code的浓缩版:

Here's a condensed version of my code:

byte[] ConvertPixelsToBytes()
{
    // imagine a picture class that creates a 24 bbp image, and has
    // a method to get an unmanaged pixel buffer.
    // imagine it also has methods for getting the width,
    // height, pitch 

    // I suppose this line could return a bad address, but 
    // I would have expected that the Bitmap constructor would have 
    // failed if it was
    System.IntPtr pPixels = picture.GetPixelData();

    System.Drawing.Bitmap bmp = new System.Drawing.Bitmap(
            picture.width(),
            picture.height(),
            picture.pitch(),
            System.Drawing.Imaging.PixelFormat.Format24bppRgb,
            pPixels );

    // This line doesn't actually free the memory, but it could be freed in a 
    // background thread
    // (2)
    picture.releasePixelData(pPixels);

    System.IO.MemoryStream memStream = new System.IO.MemoryStream();
    try
    {
        // I don't see how this line could fail, but it does
        // (3)
        bmp.Save(memStream, System.Drawing.Imaging.ImageFormat.Bmp);
        return memStream.ToArray();
    }
   catch(System.Runtime.InteropServices.ExternalException e)
   {
       // e.Message is the very helpful " A generic error occurred in GDI+."
   }
   finally
   {
       memStream.Dispose();
   }
   return new byte[0];
}

任何想法可能什么呢?我是pretty的肯定,我的像素缓冲区是正确的,它始终工作在我们的开发/测试设备和其他客户站点。

Any idea what might be going on? I'm pretty sure my pixel buffer is right, it always works on our dev/test machines and at other customer sites.

我对可能的失败原因的想法是

My thoughts on possible reasons for failure are

一个。位图构造函数不复制像素数据,但保持对它的引用,并保存失败,因为内存被释放。我不觉得MSDN文档清楚这一点,但我认为,位图复制像素数据,而不是假定它已被锁定。

a. The bitmap constructor doesn't copy the pixel data, but keeps a reference to it, and the Save fails because the memory is released. I don't find the MSDN docs clear on this point, but I assume that the Bitmap copies the pixel data rather than assume it is locked.

乙。像素数据是无效的,并且使得保存方法失败。我怀疑这一点,因为我的像素数据是每像素24位,所以据我知道这不应该是无效的。

b. The pixel data is invalid, and causes the Save method to fail. I doubt this since my pixel data is 24 Bits per pixel, so as far as I know it should not be invalid.

℃。有与.NET Framework的一个问题。

c. There's a problem with the .NET framework.

我会AP preciate其他可能的故障原因有什么想法,所以我可以添加额外的检查和记录信息,我的应用程序,所以我可以送东西进入该领域。

I would appreciate any thoughts on other possible failure reasons so I can add extra checks and logging information to my app so I can send something out into the field.

推荐答案

MSDN文档为位图的构造留下毫不怀疑:

The MSDN docs for that Bitmap constructor leave no doubt whatsoever:

备注 呼叫者负责分配和释放由SCAN0参数指定的存储块,但是,存储器不应被释放,直到相关的位图被释放。

Remarks The caller is responsible for allocating and freeing the block of memory specified by the scan0 parameter, however, the memory should not be released until the related Bitmap is released.

这篇关于GDI +的异常保存位图到一个MemoryStream的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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