One of the web applications that I work on is Instant Church Directory, a website that helps churches create a church photo directory. Since the main focus of the application is to create a photo directory, customers are uploading all sorts of images into Instant Church Directory and the application needs to correctly convert and store the images and insert them in the final output, which is a PDF.
Since the web application was launched in September 2008 we have had an issue with some Tiff images that are uploaded and throw a System.ArgumentException when the image is converted to a Jpeg.
System.ArgumentException: Parameter is not valid. at System.Drawing.Image.Save(Stream stream, ImageCodecInfo encoder, EncoderParameters encoderParams) at System.Drawing.Image.Save(Stream stream, ImageFormat format)
Today I was finally able to track it down to the Tiff image being created (or modified) by Adobe Photoshop and Adobe Photoshop is adding extra Property Items (meta data tags) into the image.
With the help of Bob Powell’s tool: Discovering the Property Items present in an image, I was able to see that the Adobe Photoshop Tiff images included several additional property items that were not present in other Tiff images that worked. Removing the tags from the original image prior to converting it to a Jpeg resolved the issue.
Here is a list of all of the Microsoft Windows GDI+ image property tags in numerical order.
The PropertyTagICCProfile is id 0x8773 and the other tag that I removed to resolve this issue is id 0x935C (this Property Item is not defined in the list above but it has a header in it with “Adobe Photoshop Document Data Block”). Property Items 0x02BC and 0x8649 are also not defined in the GDI+ list above and also are added by Adobe Photoshop but they did not cause any issues so I just left them (although if removing the first two do not work for you then try removing the other two also).
Here is the code that I used to remove the offending Property Items from the Tiff image before converting it to a Jpeg in order to prevent the GDI+ Parameter is not valid exception:
// Check if this is a Tiff file
//loop through all of the Properties
//looking for the offending property items added by Photoshop
foreach (PropertyItem pItem in bmp.PropertyItems)
//ICC Profile tag (PropertyTagICCProfile)
//Remove this tag for Tiff to be converted to Jpeg.
//Unknown tag added by Adobe Photoshop
//"Adobe Photoshop Document Data Block".
//Removing this tag for Tiff to be converted to Jpeg
//convert the image to jpeg
System.IO.MemoryStream ms = new System.IO.MemoryStream();
bmp = new Bitmap(ms);