1

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.

EDIT

Some interesting stuff as I'm troubleshooting...I added a console app to the server, and ran it using the same account as the web app, with the same code (just those couple lines), and it worked fine. I added an empty MVC application with the same couple lines, using a new application pool, and it worked fine - I changed this application pool to use the same user account as the main app, and it still worked.

I changed the new test app to the same app pool as the app, and it failed with the same exception as the regular app. So something is wrong with that application pool, but it isn't the user.

The app pools are identical as far as I can tell. And the main one recycles overnight, and I saw the problem yesterday, so a recycle didn't cut it.

If I can't figure something out, I'll reset IIS and/or the server tonight and see if that clears it up.

EDIT 2

I just tried various combinations of this (from this question) on my dummy app, as well as putting another intermediary image in there, and still nothing...

var image = System.Drawing.Image.FromStream(
    new MemoryStream(System.IO.File.ReadAllBytes(imageLocation)));

var image3 = iTextSharp.text.Image.GetInstance(
    image, System.Drawing.Imaging.ImageFormat.Png);
Community
  • 1
  • 1
Joe Enos
  • 39,478
  • 11
  • 80
  • 136
  • Have you looked at [this] (http://stackoverflow.com/questions/1772083/when-drawing-an-image-system-runtime-interopservices-externalexception-a-gener) question? – Fredrik Lundvall Jun 16 '16 at 18:58
  • @FredrikLundvall Yep, I've seen this, but if I'm reading that correctly, it shouldn't be this consistent, right? Would only happen depending on the garbage collector's mood at the time? Right now 100% of the time in my production server, I get the error, and 0% of the time in my other two. – Joe Enos Jun 16 '16 at 19:02
  • I guess you're right, it shouldn't fail all the time. – Fredrik Lundvall Jun 16 '16 at 19:07
  • Maybe something happened to the privileges of the imagefile? – Fredrik Lundvall Jun 16 '16 at 19:28
  • @FredrikLundvall Thanks, but I have 5 separate copies on the web server, including the brand new one I just copied over, and they all behave the same. But I tried anyway - I deleted the image files and re-copied them and got the same thing. – Joe Enos Jun 16 '16 at 19:37
  • Can you access the image directly in the browser? (Assuming it's published). It feels like the only reasonable issue should be the privileges to the directory or file. – Fredrik Lundvall Jun 16 '16 at 20:07
  • @FredrikLundvall Yep, the image comes up fine in the browser. – Joe Enos Jun 16 '16 at 20:08
  • If the reset of IIS fixes your problem I suggest you post an answer yourself. – Fredrik Lundvall Jun 16 '16 at 20:32
  • @FredrikLundvall Yep, I'll follow up with this question late tonight or tomorrow. – Joe Enos Jun 16 '16 at 20:33
  • I found another [question](http://stackoverflow.com/questions/788335/why-does-image-fromfile-keep-a-file-handle-open-sometimes) that might be of use. I seems to be an issue with the file handle being kept open. – Fredrik Lundvall Jun 16 '16 at 20:55
  • @FredrikLundvall See my 2nd edit - I just tried that, and no change. Still hoping I can reboot tonight and see if that helps. – Joe Enos Jun 16 '16 at 21:16

1 Answers1

1

Drives me nuts...

This was resolved with the "Microsoft solution" of turning it off and back on again - an IIS reset with iisreset /restart on the command line, and it started working again.

So I have no idea what caused the problem, or if it'll ever happen again.

Joe Enos
  • 39,478
  • 11
  • 80
  • 136