C# - Image can't be opened in Windows XP but s

2019-08-04 09:24发布

问题:

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?