Tif Manipulation:
- Verify if tif is multipage
- Split multipage tif into one page tif
Code:
public class ImageManager
{
public static bool IsMultiPageTiff(string fileFullname)
{
Image tifImage = Image.FromFile(fileFullname);
FrameDimension frameDimensions = new FrameDimension(tifImage.FrameDimensionsList[0]);
Guid objGuid = tifImage.FrameDimensionsList[0];
int numPages = tifImage.GetFrameCount(frameDimensions);
tifImage.Dispose();
return numPages > 1;
}
/// <summary>
public static ArrayList SplitTiffMultiPage(string fileFullname, bool savePages)
{
ArrayList results = new ArrayList();
Image tifImage = Image.FromFile(fileFullname);
FrameDimension frameDimensions = new FrameDimension(tifImage.FrameDimensionsList[0]);
Guid objGuid = tifImage.FrameDimensionsList[0];
string extension = Path.GetExtension(fileFullname);
string fileFullnameWithoutExt = fileFullname.Replace(extension, "");
for (int i = 0; i != tifImage.GetFrameCount(frameDimensions); i++)
{
string newTiffFullname = string.Format("{0}_{1}{2}", fileFullnameWithoutExt, i.ToString(), extension.ToUpper());
if (savePages)
SaveTiffPage(newTiffFullname, tifImage, frameDimensions, i);
results.Add(newTiffFullname);
}
tifImage.Dispose();
File.Delete(fileFullname);
return results;
}
public static void SaveTiffPage(string newTiffFullname, Image tifImage, FrameDimension frameDimensions, int pageNumber)
{
tifImage.SelectActiveFrame(frameDimensions, pageNumber);
tifImage.Save(newTiffFullname, ImageFormat.Tiff);
}
/// <summary>
/// Gets the page number.
/// </summary>
public static int GetPageNumber(Image tifImage)
{
Guid objGuid = tifImage.FrameDimensionsList[0];
FrameDimension objDimension = new FrameDimension(objGuid);
int PageNumber = tifImage.GetFrameCount(objDimension);
return PageNumber;
}
}