.NET Image.Save方法产生的非 - 在Windows 64位可重复的结果 [英] .NET Image.Save method produces non - reproducible results on Windows 64 bit

查看:186
本文介绍了.NET Image.Save方法产生的非 - 在Windows 64位可重复的结果的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用.NET框架(试图3.5安培; 4.0)来加载一个.TIFF文件并将其保存为.PNG。 我想到两个后续调用Save()方法(使用相同的TIFF文件),以产生相同的PNG文件。然而,生成的文件是,有时候'不同。

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.

在C#code以下显示问题:

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);                
}

这将显示'差异'在迭代8,32,33,73 114,155,196在Windows 64,虽然它没有在32位机器上显示的任何错误。 (我用的x86目标;与64位的目标,更是雪上加霜:差异在迭代12,13,14,15,...)

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()?

一个示例图像可以在此 FTP站点找到

A sample image can be found on this FTP site

推荐答案

我无法解释为什么发生这种情况,但现在看来,在图像中的非确定性终止终结器线程上的对象是影响图像的主线程上的编码。 (图片工具的IDisposable ,所以你应该叫处置上使用它,当你完成它确定性地清理,否则,它会在任意时间在未来定稿)

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.)

如果我更改例如code到下面,我从每次调用相同的结果为保存

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
}

请注意,我没有找到一个更奇怪:运行32位(x86)时,建立在Windows 7 SP1 64位,第一次的两个的来电保存返回不同的结果,那么每个后续调用保存制作为第二个呼叫相同的输出。为了使测试通过,我不得不重复前两行(循环之前)来强制执行相等检查前两扑救。

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.

这篇关于.NET Image.Save方法产生的非 - 在Windows 64位可重复的结果的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆