Parameter is not valid calling Bitmap.Save()

2019-08-03 15:54发布

问题:

This in in an ASP.NET application.

Locally, this code runs fine, but on our production server, it throws an exception of 'Parameter is not valid' when Bitmap.Save() is called.

Should I not be using System.Drawing.Bitmap because its not recommened based on this:

http://msdn.microsoft.com/en-us/library/system.drawing.aspx

Classes within the System.Drawing namespace are not supported for use within a Windows or ASP.NET service. Attempting to use these classes from within one of these application types may produce unexpected problems, such as diminished service performance and run-time exceptions.

What else could I use?

Bitmap myBitmap = new Bitmap(img);
myBitmap.SetResolution(img.HorizontalResolution, img.VerticalResolution);

// get the tiff codec info
ImageCodecInfo myImageCodecInfo = GetEncoderInfo("image/tiff");

// Create an Encoder object based on the GUID for the Compression parameter category
System.Drawing.Imaging.Encoder myEncoder = System.Drawing.Imaging.Encoder.Compression;

// create encode parameters
EncoderParameters myEncoderParameters = new EncoderParameters(1);
EncoderParameter myEncoderParameter = new EncoderParameter(myEncoder, (long)EncoderValue.CompressionCCITT4);
myEncoderParameters.Param[0] = myEncoderParameter;

// save as a tiff
myBitmap.Save(input, myImageCodecInfo, myEncoderParameters);

回答1:

My best guess is that the server does not have the tiff codec installed. If you are running this on Windows Server core, GDI+ is not available.



回答2:

I would verify the input stream that you are passing. I would also make sure i have the relative paths mapped appropriately to the Server Paths.



回答3:

I know this may not be helpful for everyone, as it may be feasible for some projects and not for some others.

But the way I fixed this issue, was to lower down the target from Framework Version 3.5 to 2.0.

That's the only change I did, and then it worked. Seems like the 3.5 searches for another version of the GDI+...

Another thing I wanted to try is using the patch from the KB958911, but I didn't give it a try, after the framework modification worked for me.

Perhaps if you are not able to lower the framework, you could give it a try.