C# - Image can't be opened in Windows XP but same code works in Windows 7

白昼怎懂夜的黑 提交于 2019-12-11 13:58:19

问题


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

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

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

Any ideas?

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

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

回答1:


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

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

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

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.

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



来源:https://stackoverflow.com/questions/10381768/c-sharp-image-cant-be-opened-in-windows-xp-but-same-code-works-in-windows-7

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!