System.Drawing.Image Save() producing inconsistent file sizes

1.4k views Asked by At

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:

  1. I read a TIFF image into a byte array.
  2. I use the System.Drawing.Image.FromStream() method to create an image object from the byte array.
  3. I then call the System.Drawing.Image.Save(stream) method to save the image to a new stream.
  4. 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?

2

There are 2 answers

0
DCNYAM On BEST ANSWER

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.

1
werner On

I see a similar issue on Windows 2008 server, 64bit: loading the same TIFF and saving it again will produce different files at different runs. Therfore I don't think it is specific to Windows 7. I wrote the following C# program to show it:

for (int i = 0; i < 2000; i++)
            {

                sourceToConvert = Bitmap.FromFile("c:\\tmp\\png\\zip\\fig_AAAW_6.tif");
                sourceToConvert.Save("c:\\tmp\\png\\fig_AAAW_6_regen.png", ImageFormat.Png);

                if (!CompareFileBytes("c:\\tmp\\png\\fig_AAAW_6_gen.png", "c:\\tmp\\png\\fig_AAAW_6_regen.png"))
                    MessageBox.Show("Diff" + i);                
            }

This will display 'Diff' at iteration 8, 32, 33, 73, 114, 155, 196, ... on 64 bit machines while it produces exactly the same files on 32 bit machines when compiled with x86 target CPU. When I use the 64 bit target, it gets worse: files differ at iteration 12, 13, 14, 15, ...

.