保存时“GDI +中发生一般错误”

时间:2009-11-02 03:59:50

标签: c# .net image-processing gdi+

我正在尝试“重新保存”图像并收到错误“GDI +中发生了一般错误”。我已经做了一些搜索这个错误,但还没有找到解决方案!大多数建议都提到:

  • 检查流未被使用或已锁定(即文件流)
  • 确保在整个对象的生命周期内不处理流(不应该如下所示)
  • 尝试在Bitmap对象中复制对象,并使用它来保存(对我来说不起作用)

我正在使用的代码如下:

using (Stream @imageStream = ResourceManager.CreateFile(finalResourceId, imageFileName))
{
    using (MemoryStream ms = new MemoryStream(imageFile.ResourceObject))
    {
        using (Image img = Image.FromStream(ms))
        {
            imageWidth = img.Width;
            imageHeight = img.Height;
            img.Save(@imageStream, img.RawFormat);
        }
     }
 }

在上面的代码中,ResourceManager.CreateFile返回等效的MemoryStream,因此不应出现任何“资源问题”。

我不认为其他人遇到过这个问题并且能够分享他们的解决方案吗?在此先感谢您的帮助!

1 个答案:

答案 0 :(得分:1)

感谢@Scozzard提醒我想一个解决方法!

int imageWidth, imageHeight;
using (Stream imageStream = ResourceManager.CreateFile(finalResourceId, imageFileName))
{
    using (Image img = Image.FromStream(new MemoryStream(imageFile.ResourceObject)))
    {
        imageWidth = img.Width;
        imageHeight = img.Height;
    }
    imageStream.Write(imageFile.ResourceObject, 0, imageFile.ResourceObject.Length);
}

因为我完全在内存中工作,所以我不需要使用图像对象来重新保存它,因为它是相同的图像格式 - 我可以将字节缓冲区复制到新的流中。

感谢您的评论!

相关问题