我有一个 ASP.NET-MVC 项目,我需要在其中动态生成一些 png 图像。这很容易。我只是创建返回 FileSreamResult 对象的 ActionResult。为了生成图像,我使用了 System.Drawing 中的类,例如 Bitmap 和 Image。在本地机器和生产服务器上一切正常。但是当生产服务器上的 IIS 由于不活动而关闭我的应用程序池并再次启动它时,图像生成开始失败。
问题在于尝试将图像保存到流的代码中:
var imageStream = new MemoryStream();
bmp.Save(imageStream, ImageFormat.Png);
异常是:System.Runtime.InteropServices.ExternalException (0x80004005):GDI+ 中发生一般错误。所以没有太大帮助。我尝试了不同的解决方案,但没有任何帮助。找到这个话题后Alternatives to System.Drawing for use with ASP.NET?
本主题的主要思想是:
Classes within the System.Drawing namespace are not supported for use within a Windows or ASP.NET service. Attempting to use these classes from within one of these application types may produce unexpected problems, such as diminished service performance and run-time exceptions.
所以我决定使用 WPF 类来生成图像。我重写了所有图像生成代码,但现在在应用程序池重新启动后出现另一个错误。异常(exception):
System.Runtime.InteropServices.COMException (0x88982F8A):来自 HRESULT 的异常:0x88982F8A。当我尝试将 PngBitmapEncoder 保存到流式传输时会发生这种情况
var stream = new MemoryStream();
encoder.Save(stream);
也许有人遇到过这个问题或只是有任何想法?
最佳答案
这是一个不满意的答案...
当我第一次部署新的图像生成功能时,我遇到了同样的错误。
我停止了相应的应用程序池,再次重新启动它,它工作。
关于asp.net-mvc - asp.net中奇怪的图像生成错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9631170/