【问题标题】:"A generic error occurred in GDI+" upon Save保存时“GDI+ 中发生一般错误”
【发布时间】:2010-12-12 04:12:54
【问题描述】:

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

  • 检查流未在使用或锁定(即文件流)
  • 确保在对象的生命周期内不会释放流(不应该像您将在下面看到的那样)
  • 尝试在位图对象中复制对象,并使用它来保存(对我不起作用)

我使用的代码如下:

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,因此不应该有任何“资源问题”。

我想没有其他人遇到过这个问题并且能够分享他们的解决方案吗?提前感谢您的帮助!

【问题讨论】:

  • 如您所知,您可以将这些 using 语句组合起来。
  • 您保存的文件夹是否具有 ASPNET_MACHINE 和 NETWORKSERVICE 写入权限?
  • 您好,感谢您对 using 语句的建议。至于权限查询 - 这些不应该发挥作用,因为我没有写入文件系统,而是将所有内容保存在内存中。

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


【解决方案1】:

感谢@Scozard 提示我考虑解决方法!

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);
}

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

感谢您的 cmets!

【讨论】:

    猜你喜欢
    • 2019-01-02
    • 2013-09-26
    • 1970-01-01
    • 2015-07-02
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多