Here is the class that I created for TIFF extraction. It is a work in progress. It's sole purpose is to allow you to extract arbitrary pages from a multipage TIFF and store it as a single page image. It supports both Tiffs and PNGs, although I'm not sure there is a such a thing as a multipage PNG.
I use this class in TC vis the CLR bridge.
Usage: Use the static CreateWrappedTiff method to create a WrappedTiff. It takes the path filename of the multipage TIFF. I did this because I find using constructors in .NET classes to be rather painful in TC.
Use the NumPages method on a WrappedTiff instance to get the number of pages in the file.
Use ExtractPage to extract a page from the TIFF. It saves the image at the specified index (0-based) to the filename specified.
After extracting all the images you want, call Destroy() on the object to release the bitmap and stream associated with the input file. (This should probably use dispose).
I used TC to do the actual image comparisons by creating various Picture objects and comparing those.
using System.Drawing;
using System.Drawing.Imaging;
using System.IO;
public class WrappedTiff
{
Bitmap b;
Stream bitmapStream;
ImageFormat format;
public static WrappedTiff CreateWrappedTiff(string filename)
{
WrappedTiff w = new WrappedTiff();
// Bitmap.FromFile doesn't release the file after reading, read from a stream instead
w.bitmapStream = File.OpenRead(filename);
w.b = (Bitmap)Bitmap.FromStream(w.bitmapStream);
if (filename.ToUpper().EndsWith(".TIF"))
{
w.format = ImageFormat.Tiff;
}
else
{
w.format = ImageFormat.Png;
}
return w;
}
public int NumPages()
{
return b.GetFrameCount(System.Drawing.Imaging.FrameDimension.Page);
}
public bool ExtractPage(string outputfile, int page)
{
if (page >= NumPages())
{
return false;
}
b.SelectActiveFrame(FrameDimension.Page, page);
b.Save(outputfile, this.format);
return true;
}
public void Destroy()
{
bitmapStream.Close();
b = null;
}
}