I am trying to improve the speed at which I am able to split a multi-page TIFF file into it's individual pages, stored as a list of byte arrays. I have this TiffSplitter class that I'm working on, to try and improve the speed of the Paginate method.
I have heard of LibTiff.net, and wonder if it would be any faster than this process? Currently, it takes about 1333 ms to call the Paginate method on a 7-page multipage TIFF file.
Does anyone know what would be the most efficient way to retrieve the individual pages of a multipage TIFF as byte arrays? Or possibly have any suggestions as to how I can improve the speed of the process I'm currently using?
using System;
using System.Collections.Generic;
using System.Linq;
using System.Drawing;
using System.Drawing.Imaging;
using System.IO;
namespace TiffSplitter
{
public class TiffPaginator
{
private List<byte[]> paginatedData;
public List<byte[]> Pages
{
get
{
return paginatedData;
}
}
public TiffPaginator()
{
paginatedData = new List<byte[]>();
}
public void Paginate(string Filename)
{
using (Image img = Image.FromFile(Filename))
{
paginatedData.Clear();
int frameCount = img.GetFrameCount(FrameDimension.Page);
for (int i = 0; i < frameCount; i++)
{
img.SelectActiveFrame(new FrameDimension(img.FrameDimensionsList[0]), i);
using (MemoryStream memstr = new MemoryStream())
{
img.Save(memstr, ImageFormat.Tiff);
paginatedData.Add(memstr.ToArray());
}
}
}
}
}
}
I tried using the LibTiff.net, and for me, it was quite slow. The time to split a singe 2-page tif was measured in seconds. In the end, I decided to reference PresentationCore and go with this: (It splits the images to multiple files, but it should be simple to switch the output to byte arrays)
Stream imageStreamSource = new FileStream("filename", FileMode.Open, FileAccess.Read, FileShare.Read);
TiffBitmapDecoder decoder = new TiffBitmapDecoder(imageStreamSource, BitmapCreateOptions.PreservePixelFormat, BitmapCacheOption.Default);
int pagecount = decoder.Frames.Count;
if (pagecount > 1)
{
string fNameBase = Path.GetFileNameWithoutExtension("filename");
string filePath = Path.GetDirectoryName("filename");
for (int i = 0; i < pagecount; i++)
{
string outputName = string.Format(@"{0}\SplitImages\{1}-{2}.tif", filePath, fNameBase, i.ToString());
FileStream stream = new FileStream(outputName, FileMode.Create, FileAccess.Write);
TiffBitmapEncoder encoder = new TiffBitmapEncoder();
encoder.Frames.Add(decoder.Frames[i]);
encoder.Save(stream);
stream.Dispose();
}
imageStreamSource.Dispose();
}
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.