C#-无法在Windows XP中打开图像,但在Windows 7中可以使用相同的代码 [英] C# - Image can't be opened in Windows XP but same code works in Windows 7

查看:112
本文介绍了C#-无法在Windows XP中打开图像,但在Windows 7中可以使用相同的代码的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有两个tiff图像,一个是黑白图像,另一个是灰度图像8.

I have two tiff images, one is black and white, the other is grayscale8.

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

I need to show them in picture box, when I try to open them:

Image.FromFile("path");

BW一个打开都没有问题,灰度一个给我一个例外:内存不足"

The BW one opens with no issues, the grayscale one give me an exception: "out of memory"

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

That only happens when I execute the code in a WinXP SP3 machine, a colleague with a Windows 7 has no problems in both cases

有什么想法吗?

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

More info: MS Paint and standard Microsoft Image Viewer can't open the grayscale image, while Office Picture Manager can

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

Windows 7 can open image with any softwate

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

I have this temporal solution, but I think is not the best:

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

推荐答案

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

Image.FromFile uses the native GDI calls to load the image.

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

There are quite a few reports of out of memory exceptions when loading unsupported TIFF files.

http:///social.msdn.microsoft.com/Forums/zh-CN/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中可能已经添加了对其他格式的支持.

The TIFF file format has many encoding options not all of which are supported naively by Windows. TIFF files are a bit like AVI files in that the contents can be compressed in different ways. Support for other formats could have been added in Windows 7.

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

Is it possible to change the encoding options on your TIFF file?

这篇关于C#-无法在Windows XP中打开图像,但在Windows 7中可以使用相同的代码的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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