Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Strange image generation error in asp.net

I have an ASP.NET-MVC project where I need to dynamically generate some png images. That's easy. I just create ActionResult that returns FileSreamResult object. To generate images I used classes from System.Drawing such as Bitmap and Image. Everything works fine on local machine and on production server. But when IIS on production server shuts down my application pool due to inactivity and starts it again, image generation starts to fail.

Problem was in code that tries to save image to the stream:

  var imageStream = new MemoryStream();
  bmp.Save(imageStream, ImageFormat.Png);

Exception is: System.Runtime.InteropServices.ExternalException (0x80004005): A generic error occurred in GDI+. So there is no much help. I tried a different solutions and nothing helps. After I have found this topic Alternatives to System.Drawing for use with ASP.NET?

The main idea of this topic is:

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.

So I decided to use WPF classes to generate images. I rewrote all image generation code, but now I get another error after app pool restart. Exception: System.Runtime.InteropServices.COMException (0x88982F8A): Exception from HRESULT: 0x88982F8A. This happens when I try to save my PngBitmapEncoder to stream

var stream = new MemoryStream();
encoder.Save(stream);

Maybe anyone encountered this problem or just have any ideas?

like image 244
Aleksandr Ivanov Avatar asked Nov 14 '22 09:11

Aleksandr Ivanov


1 Answers

Here's an unsatisfactory answer...

I was getting the same error when I first deployed my new image generation functionality.

I stopped the corresponding application pool, restarted it again, and it worked.

like image 73
Andrew Shepherd Avatar answered Dec 04 '22 22:12

Andrew Shepherd