0

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?

DrMistry
  • 321
  • 1
  • 13
  • What are your development machine and server's OS versions? There are more issues related to `System.Drawing.Imaging` (GDI+), like [letters not being rendered](http://stackoverflow.com/questions/15526124/net-system-drawing-differences-in-windows-server-2008r2-64bit-compared-to-all-o), [image quality differing](http://stackoverflow.com/questions/20069100/) and other [graphics issues](http://stackoverflow.com/questions/7649288/). Try to find the offending setting and tweak the parameters until you have a small example reproducing the problem. – CodeCaster Dec 15 '14 at 20:13
  • The server is Windows Server 2008 R2 and the dev machine is Windows 7 x64 - updated question with info. – DrMistry Dec 15 '14 at 20:16
  • Saving the full-size image with the same encoderparams as the thumbs has made no difference - I've even tried using the GetThumbnail method to produce a thumbnail of exactly the same size as the original and it's still dimmed. Tried setting all the parameters I can think of for the encoder and the result is the same there also. – DrMistry Dec 15 '14 at 20:39

1 Answers1

0

In this instance, Bitmap.Save was saving the "unaltered" image as 48bpp rather than the original 32bpp. I'm having to force it to 32, but I need to test with a number of other images with various bitdepths.

DrMistry
  • 321
  • 1
  • 13