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);