While converting TIFFs to PDFs, I noticed some of the PDFs were corrupted. After some research, it appears the problem is in the System.Drawing.Image class. To test this, instead of converting to PDFs, I had the program just read in the image files and re-save them. Some of the newly saved files have inconsistent file sizes between different runs of the program. The basic steps are:
- I read a TIFF image into a byte array.
- I use the System.Drawing.Image.FromStream() method to create an image object from the byte array.
- I then call the System.Drawing.Image.Save(stream) method to save the image to a new stream.
- I then examine the length of the stream.ToArray() method.
The same input file results in a different output length between successive program executions. The output length varies by a couple hundred bytes. In addition, the resulting output length is more than twice the size of the input length, but I assume this is due to compression, or lack thereof. I am running this on windows 7 32-bit with .net 4.
Why might the output length vary like this?
UPDATE:
After reviewing this connect issue (https://connect.microsoft.com/VisualStudio/feedback/details/584681/system-drawing-image-flags-has-different-value-in-vista-and-windows-7) and the community comment on this MSDN page (http://msdn.microsoft.com/en-us/library/system.drawing.image.save.aspx), it appears the issue is related to an operating system level bug in Windows 7. Can anyone confirm this or offer a workaround?
As stated in my update, after reviewing this connect issue (https://connect.microsoft.com/VisualStudio/feedback/details/584681/system-drawing-image-flags-has-different-value-in-vista-and-windows-7) and the community comment on this MSDN page (http://msdn.microsoft.com/en-us/library/system.drawing.image.save.aspx), it appears the issue is related to an operating system level bug in Windows 7.
In addition, when the images are read in Windows XP, the flags property on the image object is set to 77888. On Win7, it is set to 77840. After reviewing the MSDN documentation for the flags property (http://msdn.microsoft.com/en-us/library/system.drawing.image.flags.aspx), the difference is that WinXP flagged the image as a grayscale image (which mine are), but Win7 flagged it as an RGB image. This appears to be a symptom of the problem, but I don't know enough about image formats and color spaces to speak with authority on this.