【问题标题】:Parameter is not valid calling Bitmap.Save()参数无效调用 Bitmap.Save()
【发布时间】:2011-03-22 20:38:22
【问题描述】:

这在 ASP.NET 应用程序中。

在本地,这段代码运行良好,但在我们的生产服务器上,当调用 Bitmap.Save() 时,它会抛出“参数无效”异常。

我应该不使用 System.Drawing.Bitmap,因为它不推荐基于此:

http://msdn.microsoft.com/en-us/library/system.drawing.aspx

不支持使用 System.Drawing 命名空间中的类 在 Windows 或 ASP.NET 服务中。 尝试使用这些类 在这些应用程序类型之一中 可能会产生意想不到的问题,例如 由于服务性能下降和 运行时异常。

我还能用什么?

Bitmap myBitmap = new Bitmap(img);
myBitmap.SetResolution(img.HorizontalResolution, img.VerticalResolution);

// get the tiff codec info
ImageCodecInfo myImageCodecInfo = GetEncoderInfo("image/tiff");

// Create an Encoder object based on the GUID for the Compression parameter category
System.Drawing.Imaging.Encoder myEncoder = System.Drawing.Imaging.Encoder.Compression;

// create encode parameters
EncoderParameters myEncoderParameters = new EncoderParameters(1);
EncoderParameter myEncoderParameter = new EncoderParameter(myEncoder, (long)EncoderValue.CompressionCCITT4);
myEncoderParameters.Param[0] = myEncoderParameter;

// save as a tiff
myBitmap.Save(input, myImageCodecInfo, myEncoderParameters);

【问题讨论】:

  • 你能说明一下GetEncoderInfo(string)的功能吗?
  • 服务器缺少 tiff 编码器,但这还没有结束。就像 MSDN 文档所说的那样,System.Drawing 类不应该在服务中使用。我有一些第三方图像控件(Atalasoft),我用它来保存到 tiff。

标签: c# .net asp.net asp.net-4.0


【解决方案1】:

我最好的猜测是服务器没有安装 tiff 编解码器。如果您在 Windows Server 核心上运行它,则 GDI+ 不可用。

【讨论】:

    【解决方案2】:

    我会验证您传递的输入流。我还会确保将相对路径正确映射到服务器路径。

    【讨论】:

      【解决方案3】:

      我知道这可能对每个人都没有帮助,因为它可能对某些项目可行,而对另一些项目则不可行。

      但我解决此问题的方法是将目标从 Framework 版本 3.5 降低到 2.0。

      这是我做的唯一改变,然后它起作用了。好像 3.5 搜索另一个版本的 GDI+...

      我想尝试的另一件事是使用来自KB958911 的补丁,但在框架修改对我有用之后,我没有尝试。

      也许如果你不能降低框架,你可以尝试一下。

      【讨论】:

        猜你喜欢
        • 2013-02-16
        • 1970-01-01
        • 2017-12-02
        • 1970-01-01
        • 2011-02-04
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多