C # Generic GDI + Error when using Image.Save ()

I am a relative newbie with an image in C #. This is my first question on this board after a very long membership time. Hope this helps me overcome this difficult scenario.

I need to read the contents (frames) of a multi-page TIFF, saving each of them in a list and finally returning it, then do some work with it.

Here is my code so far

public static List<Image> GetAllPages(string file) { images = new List<Image>(); using (Image img = Image.FromFile(file)) { try { for (int i = 0; i < img.GetFrameCount(FrameDimension.Page); i++) { img.SelectActiveFrame(FrameDimension.Page, i); MemoryStream byteStream = new MemoryStream(); img.Save(byteStream, ImageFormat.Tiff); images.Add(Image.FromStream(byteStream)); } } catch (Exception ex) { MessageBox.Show(ex.Message); } } return images; } 

IMPORTANT Actually, it works like a charm when executed on Windows 7. However, when I try to do the same in Windows XP, I get the seemingly well-known Generic GDI + error.

Is there something obvious I'm missing here? If not, would there be some other, more efficient way to return a list of images extracted from multipage tiff?

I would really appreciate any help anyone can give.

+1
source share
1 answer

Hans Passan's commentary has 2 useful bits of information. First, the TIFF format supports many subtypes and options, not all of which are supported by GDI +. Secondly, GDI + has been improved after XP, but not everywhere.

Your code works on both Windows XP and Windows 7, but only with the correct TIFF input files.

I tested it using .NET 2.0 and .NET 4.0 with 2 input files. The first file had 6 pages, all of which were encoded with LZW compression. This file worked on both Windows 7 and XP.

The second file had 11 pages with 11 different types of encoding. Only 7 pages out of 11 were loaded in Windows XP. Even fewer pages worked in Windows 7, but JPEG compression, in particular, worked correctly, although it did not work with XP.

Windows 8.1 is better than both, and it succeeded in loading 8 pages correctly.

Files can be found along with a list of compression types used on this page: http://support.leadtools.com/CS/forums/44475/ShowPost.aspx

Of course, there are other TIFF subtypes, most of which are not natively supported by GDI +, but these 11 are the most common formats.

This leaves us with a problem so that your files work in Windows XP. Since they work on Windows 7, there is a high probability that you have a TIF subtype that works on Windows 7 but not XP, such as JPEG compression. If this is the case, .NET alone will not be enough, and you may have to use a dedicated image library or TIFF to load such files.

+1
source

All Articles