jeudi 16 juin 2016

A generic error occurred in GDI+ with iTextSharp, only on a particular server

Most of the questions about this error (and wow, there are a lot) revolve around saving to a file, which isn't what I'm doing.

My code is as follows - .NET 4.5, MVC3, IIS7.5, Win2008R2:

string fileName = /* something.png */;
string imageLocation = HttpContext.Current.Server.MapPath(
    string.Format(@"~/Content/images/{0}", fileName));

var image = iTextSharp.text.Image.GetInstance(
    System.Drawing.Image.FromFile(imageLocation),
    System.Drawing.Imaging.ImageFormat.Png);

Decompiling iTextSharp, the GetInstance method looks like:

public static Image GetInstance(Image image, ImageFormat format)
{
    MemoryStream memoryStream = new MemoryStream();

    image.Save(memoryStream, format); // <-- Exception here

    return Image.GetInstance(memoryStream.ToArray());
}

The exception I get is:

System.Runtime.InteropServices.ExternalException (0x80004005):

A generic error occurred in GDI+.

at System.Drawing.Image.Save(Stream stream, ImageCodecInfo encoder, EncoderParameters encoderParams)

at iTextSharp.text.Image.GetInstance(Image image, ImageFormat format)

So it's failing when attempting to save the image into a MemoryStream, not to a file or the HTTP response.

This just started happening to an existing application that worked fine previously. The exact same code functions perfectly on a different server and my local machine (all 3 are 2008R2), and it happens to every application on my production server (I have 4), and also even a brand new one with the same codebase I put up there for testing this issue.

So I assume there's something with my specific production server, maybe IIS. Windows updates seem to be the same between the two servers, so I don't think that's it.

Anything else that you can think of which might be environment-specific to cause this error? I can re-work the code to clean up our part a bit, like creating a new in-memory image instead of using the one created from the file, but since this is my production system, I'd rather find a short-term solution without a code-fix.

Aucun commentaire:

Enregistrer un commentaire