.NET Image.Save method produces non - reproducible results on Windows 64 bit

痞子三分冷 提交于 2019-12-04 20:45:24

问题


I'm using .NET framework (tried 3.5 & 4.0) to load a .TIFF file and save it as .PNG. I expect two subsequent calls to the Save() method (using the same TIFF file) to produce the same PNG file. The produced files are, however, 'sometimes' different.

The C# code below shows the problem:

Image sourceToConvert = Bitmap.FromFile("c:\\tmp\\F1.tif");
sourceToConvert.Save("c:\\tmp\\F1_gen.png", ImageFormat.Png);           

for (int i = 0; i < 100; i++)
{
    sourceToConvert = Bitmap.FromFile("c:\\tmp\\F1.tif");
    sourceToConvert.Save("c:\\tmp\\F1_regen.png", ImageFormat.Png);

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

This will display 'Diff' at iteration 8, 32, 33, 73 114, 155, 196 on Windows 64, while it does not display any errors on 32 bit machines. (I use x86 target; with x64 target, it is worse: diff at iteration 12, 13, 14, 15, ...)

Is there a way to get a reproducible result from Save()?

A sample image can be found on this FTP site


回答1:


I can't explain why this is happening, but it appears that non-deterministic finalization of the Image objects on the finalizer thread is affecting the encoding of images on the main thread. (Image implements IDisposable, so you should call Dispose on it to deterministically clean it up when you're finished using it; otherwise, it will be finalized at an arbitrary time in the future.)

If I change your example code to the following, I get the same results from every call to Save:

using (Image sourceToConvert = Bitmap.FromFile("c:\\tmp\\F1.tif"))
    sourceToConvert.Save("c:\\tmp\\F1_gen.png", ImageFormat.Png);           

for (int i = 0; i < 100; i++)
{
    using (Image sourceToConvert = Bitmap.FromFile("c:\\tmp\\F1.tif"))
        sourceToConvert.Save("c:\\tmp\\F1_regen.png", ImageFormat.Png);

    // files are the same
}

Note that I did find one further oddity: when running a 32-bit (x86) build on Windows 7 SP1 x64, the first two calls to Save returned different results, then every subsequent call to Save produced the same output as the second call. In order to make the test pass, I had to repeat the first two lines (before the loop) to force two saves before performing the equality checks.



来源:https://stackoverflow.com/questions/10586538/net-image-save-method-produces-non-reproducible-results-on-windows-64-bit

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!