【问题标题】:C# - Image can't be opened in Windows XP but same code works in Windows 7C# - 图像无法在 Windows XP 中打开,但相同的代码在 Windows 7 中有效
【发布时间】:2012-04-30 10:00:35
【问题描述】:

我有两张tiff图片,一张是黑白的,一张是grayscale8的。

当我尝试打开它们时,我需要在图片框中显示它们:

Image.FromFile("path");

BW 的打开没有问题,灰度的给我一个异常:“内存不足”

只有当我在 WinXP SP3 机器上执行代码时才会发生这种情况,使用 Windows 7 的同事在这两种情况下都没有问题

有什么想法吗?

更多信息:MS Paint 和标准 Microsoft Image Viewer 无法打开灰度图像,而 Office Picture Manager 可以

Windows 7 可以用任何软件打开图片

我有这个临时解决方案,但我认为不是最好的:

System.Windows.Media.Imaging.BitmapImage bImg = null;

using (var fs = new FileStream(dlg.FileName, FileMode.Open))
{
    bImg = new System.Windows.Media.Imaging.BitmapImage();
    bImg.BeginInit();
    bImg.StreamSource = fs;
    bImg.EndInit();
}

if (bImg.Format == System.Windows.Media.PixelFormats.Gray8)
{
    Bitmap bitmap;

    using (MemoryStream outStream = new MemoryStream())
    {
        BitmapEncoder enc = new BmpBitmapEncoder();
        enc.Frames.Add(BitmapFrame.Create(bImg));
        enc.Save(outStream);
        bitmap = new System.Drawing.Bitmap(outStream);
    }
    AssignImage(bitmap);
}
else
    AssignImage(Image.FromFile(dlg.FileName));

【问题讨论】:

    标签: c# windows-7 windows-xp gdi+ tiff


    【解决方案1】:

    Image.FromFile 使用本机 GDI 调用来加载图像。

    在加载不受支持的 TIFF 文件时,有很多关于内存不足异常的报告。

    http://social.msdn.microsoft.com/Forums/en-US/winformsdesigner/thread/582c0a29-97ef-4136-8a7f-81eb1b1f1c94/

    http://www.pcreview.co.uk/forums/opening-tiff-file-throws-out-memory-exception-t3105542.html

    TIFF 文件格式有许多编码选项,并非所有这些选项都被 Windows 原生支持。 TIFF 文件有点像 AVI 文件,因为它的内容可以以不同的方式压缩。 Windows 7 中可能已添加对其他格式的支持。

    是否可以更改 TIFF 文件的编码选项?

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2012-09-20
      • 2011-05-25
      • 1970-01-01
      • 1970-01-01
      • 2014-02-06
      • 1970-01-01
      • 2012-06-09
      • 1970-01-01
      相关资源
      最近更新 更多