【问题标题】:Converting Tif to Jpg takes way too long将 Tif 转换为 Jpg 需要很长时间
【发布时间】:2013-10-09 07:06:03
【问题描述】:

我正在尝试将许多(1000 多张)图像从 tiff 转换为 jpg,但在 appr. 250-300 张图像,任何其他图像大约需要 5-10 秒,即使前 250 张需要 20 秒。

这是我使用的代码:

foreach (string filePath in Directory.GetFiles(tifPath, "*.tif", SearchOption.AllDirectories))
{
    System.Drawing.Image.FromFile(filePath).Save(jpgPath + "\\" + Path.GetFileNameWithoutExtension(filePath) + ".jpg", ImageFormat.Jpeg);
}

我的方法有问题吗?提前致谢。

【问题讨论】:

  • 您可以将从 Directory.GetFiles 收到的文件保存到一个变量中,这样程序就不必多次执行该搜索
  • @HimBromBeere 这不是问题所在。问题是由于未释放的资源导致内存泄漏

标签: c# jpeg tiff


【解决方案1】:

图像需要被释放,否则它会留在内存中:

foreach (string filePath in Directory.GetFiles(tifPath, "*.tif", SearchOption.AllDirectories))
{
    using (var image = System.Drawing.Image.FromFile(filePath))
    {
        image.Save(jpgPath + "\\" + Path.GetFileNameWithoutExtension(filePath) + ".jpg", ImageFormat.Jpeg);
    }
}

有关使用语句的更多信息,请参阅此站点:

http://www.dotnetperls.com/using

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-08-22
    • 2018-05-27
    • 2011-12-04
    • 2018-10-10
    • 2013-09-07
    相关资源
    最近更新 更多