Parameter is not valid calling Bitmap.Save()
Asked Answered
C

3

1

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);
Clevie answered 22/3, 2011 at 20:38 Comment(2)
Could you please show what the function GetEncoderInfo(string) does?Torey
It was that the tiff encoder was missing from the server, but that wasn't the end of it. Like the MSDN doc's say, the System.Drawing classes should not be used in services. I have some third party image controls (Atalasoft) and I used that to save it to a tiff.Clevie
H
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.

Housemaster answered 22/3, 2011 at 20:50 Comment(0)
S
0

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.

Save answered 22/3, 2011 at 20:53 Comment(0)
V
0

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.

Visionary answered 14/8, 2014 at 10:20 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.