加载,保存,然后再次加载图像会引发“GDI +中发生一般错误”。 [英] Loading, saving, and then loading an image again throws "A generic error occurred in GDI+"

查看:103
本文介绍了加载,保存,然后再次加载图像会引发“GDI +中发生一般错误”。的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

似乎存在臭名昭着的错误。我记得在不久的代码中得到了一段时间,但它回来了,复仇,但有一些我似乎无法弄清楚的新代码。

它是肯定是 generic

This seems to be in infamous error. I remember getting it a while back for different code, but it's back, with a vengeance, but with some new code that I can't seem to figure out.
It's definitely generic, for sure!

我正在尝试构建一个允许用户进行以下操作的表单:

I'm trying to build a form that allows a user to:


  1. 选择图像。

  2. 按save(关闭表单),并将图像保存到 byte [] 到数据库中。

  3. 打开form(从 byte [] 加载图像。)。

  4. 允许他们再次按save。

  5. 允许他们再次打开表单,再次显示图像。

  1. Select an image.
  2. Press save (which closes the form), and saves the image to a byte[] into a database.
  3. Open the form (which loads the image from the byte[]).
  4. Allow them to press save again.
  5. Allow them to open the form again, displaying the image (again).

一个非常标准的加载/保存方案。

A pretty standard load/save scenario.

关于加载和保存到SQL Server,一切正常。我得到的问题是反复加载和保存到 byte [] ,即使我使用相同的设置。看看我为了证明问题而嘲笑的代码:

Everything works fine regarding loading and saving to the SQL Server. The problem I'm getting is to do with repeatedly loading and saving to and from a byte[] even though I'm using the same settings. Take a look at this code which I mocked up to demonstrate the problem:

static void Main(string[] args)
{
    // Load the image
    var initialImage = (Bitmap)Bitmap.FromFile(@"D:\picture.jpg");

    // Save to a memory stream and get the bytes
    var initialImageBytes = SaveImageToBytes(initialImage);

    // Load again from this saved image
    Bitmap byteLoadedImage = LoadImageFromBytes(initialImageBytes);

    // Save again to bytes, throws "A generic error occurred in GDI+."
    var secondaryImageBytes = SaveImageToBytes(byteLoadedImage);
}

private static byte[] SaveImageToBytes(Bitmap image)
{
    byte[] imageBytes;
    using (MemoryStream imageStream = new MemoryStream())
    {
        image.Save(imageStream, image.RawFormat);
        // "A generic error occurred in GDI+." thrown when saved second time
        imageBytes = imageStream.ToArray();
    }

    return imageBytes;
}

private static Bitmap LoadImageFromBytes(byte[] bytes)
{
    using (MemoryStream imageStream = new MemoryStream(bytes))
    {
        Bitmap image = (Bitmap)Bitmap.FromStream(imageStream);
        return image;
    }
}

错误一般错误发生在GDI +。,因为第二次将图像再次保存到 MemoryStream 。我通过检查第一次保存之前和第二次保存之前的值来检查这与 RawFormat 无关:

The error A generic error occurred in GDI+. is thrown as the image is saved once more to the MemoryStream the second time round. I checked this wasn't to do with the RawFormat by inspecting the value before the first save, and before the second:

1st Save : {b96b3cae-0728-11d3-9d7b-0000f81ef32e}
2nd Save : {b96b3cae-0728-11d3-9d7b-0000f81ef32e}

值是相同的,所以它丢失 ImageFormat 信息。

The values are identical, so it can't be a problem with it losing the ImageFormat information.

任何人都可以帮忙调试这个问题吗?我使用的代码示例使用JPEG进行测试,您可以在此处获取

Can anyone help debug this problem? The code sample I used is tested with a JPEG, and you can get it here.

推荐答案

我最近认为是同样的问题。您需要跳过创建MemoryStream的using语句。创建位图会保留对创建它的流的引用。您可以在 MSDN 上阅读相关内容。

I had what I believe was the same problem recently. You need to skip the using statement around the creation of your MemoryStream. Creating a bitmap keeps a reference to the stream that created it. You can read about it on MSDN.

private static Bitmap LoadImageFromBytes(byte[] bytes)
{
    var imageStream = new MemoryStream(bytes))
    var image = (Bitmap)Bitmap.FromStream(imageStream);
    return image;
}

这篇关于加载,保存,然后再次加载图像会引发“GDI +中发生一般错误”。的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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