C# - 图像无法在Windows XP中打开,但相同的代码在Windows 7中有效
本文关键字:Windows 有效 代码 图像 XP | 更新日期: 2023-09-27 18:32:36
我有两个tiff图像,一个是黑白的,另一个是灰度8。
当我尝试打开它们时,我需要在图片框中显示它们:
Image.FromFile("path");
BW 一个打开没有问题,灰度给我一个例外:"内存不足"
只有当我在WinXP SP3机器中执行代码时,才会发生这种情况,使用Windows 7的同事在这两种情况下都没有问题
。有什么想法吗?
详细信息:MS 画图和标准Microsoft图像查看器无法打开灰度图像,而 Office 图片管理器可以
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));
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 文件的编码选项?