In my MVC 5 web project, I'm saving bitmaps posted as part of a form to a folder on the server. While saving the image I'm also creating 2 differently sized thumbnails of the uploaded image. All three image files are successful created, but the full-sized, unmodified image saves as a "dimmed" version of the original, and is served as such. By "dimmed" I literally mean that the file which is saved full-size and unmolested appears as if a 50% transparent black image layer has been saved on top of the original.
Here's the code to save the images to disk:
private ObservationImage CreateObservationImage(UploadedFileInfo file, Observation obs)
{
Guid guid = Guid.NewGuid();
string p = Path.Combine(Server.MapPath("~/ObservationImages/"), guid.ToString() + ".png");
string thumb = Path.Combine(Server.MapPath("~/ObservationImages/thumbnails/"), guid.ToString() + ".png");
string mid = Path.Combine(Server.MapPath("~/ObservationImages/midsize/"), guid.ToString() + ".png");
Bitmap thisBitmap = new Bitmap(file.File.InputStream);
ImageCodecInfo codecInfo = GetEncoderInfo("image/png");
Encoder qualityEncoder = Encoder.Quality;
Encoder colourDepthEncoder = Encoder.ColorDepth;
Encoder compressionEncoder = Encoder.Compression;
EncoderParameters myEncoderParameters = new EncoderParameters(3);
EncoderParameter myQualityEncoderParameter = new EncoderParameter(qualityEncoder, 100L); // 100% quality
EncoderParameter myColourDepthEncoderParameter = new EncoderParameter(colourDepthEncoder, 24L); // 24 bpp
EncoderParameter myCompressionEncoderParameter = new EncoderParameter(compressionEncoder, (long)EncoderValue.CompressionNone); // no compression
myEncoderParameters.Param[0] = myQualityEncoderParameter;
myEncoderParameters.Param[1] = myColourDepthEncoderParameter;
myEncoderParameters.Param[2] = myCompressionEncoderParameter;
double ratio = (double)thisBitmap.Height / (double)thisBitmap.Width;
Image.GetThumbnailImageAbort myCallback = new Image.GetThumbnailImageAbort(ThumbnailCallback);
Image thisThumbnail = thisBitmap.GetThumbnailImage(120, (int)(120 * ratio), myCallback, IntPtr.Zero);
thisThumbnail.Save(thumb, codecInfo, myEncoderParameters);
myCallback = new Image.GetThumbnailImageAbort(ThumbnailCallback);
Image thisMidsize = thisBitmap.GetThumbnailImage(400, (int)(400 * ratio), myCallback, IntPtr.Zero);
thisMidsize.Save(mid);
thisBitmap.Save(p, codecInfo, myEncoderParameters);
ObservationImage result = new ObservationImage
{
ImageLocalPath = p,
ImageURL = "/ObservationImages/" + guid.ToString() + ".png",
ThumbnailURL = "/ObservationImages/thumbnails/" + guid.ToString() + ".png",
MidSizeUrl = "/ObservationImages/midsize/" + guid.ToString() + ".png",
ObservationId = obs.ObservationId
};
return result;
}
The UploadedFileInfo type comes from a custom model binder which provides access to the HttpPostedFileBase for the uploaded file. The thisThumbnail and thisMidsize files save absolutely as they should, thisBitmap saves as described.
Now, this all works absolutely fine on the development web server which VS2013 uses, but when deployed to an IIS7.5 server, the problem of the "dimmed" image happens. I've tried forcing every encoder parameter I can think of, to no avail. The thumbnails are saved with no ill-effects whatsoever.
Some files do upload correctly but there is no correlation that I can see - for instance the original file type and size seem to have no baring on the problem. This really has be scratching my head wondering what's going on. There are no out-of-the-ordinary settings on the IIS box (Windows Server 2008 R2), and as stated earlier it works 100% of the time on VS2013 running on my dev machine (Windows 7 x64).
Anyone have any pointers as to where I should be looking?