Related SO Answer (why is this??)
I'm calling a webservice (that I do not have access to source for) that is returning to me a byte array that is supposed to represent a multi-page TIFF image. (method names changed to preserve employment...)
var tiffBytes = _webServiceClient.GetTiff("someIdentifier");
If I write tiffBytes to a file using standard FileStream, then open the resulting file in something that knows what a TIFF is, there's no problem and I can flip through the pages properly. When I create an Image object from this file directly (Image.FromFile(...)), there is also no problems with the rest of my code. This leads me to believe that the bytes stored in tiffBytes are actually valid.
If I write tiffBytes to a MemoryStream, things get weird:
//Note that this code works just fine if I create "img" with Image.FromFile()
var frameCount = img.GetFrameCount(FrameDimension.Page); // frameCount = 2
img.SelectActiveFrame(FrameDimension.Page, 0); // First frame selected with no issue
img.SelectActiveFrame(FrameDimension.Page, 1); // Exception: "A generic error occured in GDI+"
I've considered that I was creating my MemoryStream incorrectly, but I really don't think I am. I've tried a couple of different ways with the same results; the resulting Image object will display and operate fine until I attempt to move to the last page:
Method 1:
using(var ms = new MemoryStream(tiffBytes))
{
img = Image.FromStream(ms);
}
Method 2:
using(var ms = new MemoryStream(tiffBytes.Length))
{
ms.Write(tiffBytes, 0, tiffBytes.Length);
img = Image.FromStream(ms);
}
So... my question is why does the difference between Image.FromFile() and Image.FromStream() make TIFF's last page/Frame cause an exception when it is loaded?
Side question... Is there some kind of win32 api I can access to see exactly what that exception is?