I'm writing a C# application that handles TIFF images (mainly displaying files, reordering pages, deleting pages, splitting multipage images, merging single images into one multipage image etc).
Most of the images we deal with are smaller (both in file size and in page numbers), but there are the odd larger ones.
When displaying the image, we need to split any multipage TIFF files into a List so the thumbnails can be displayed in a ListView. The issue we face is that for larger files, it takes way too long to perform the split. E.g. I just tested a 304 page image (which is only 10mb) and to split all the pages into the list took 137,145ms (137.14 seconds, or 2m 17s) which is far too slow.
The code I'm using is:
private void GetAllPages(string file)
{
System.Diagnostics.Stopwatch watch = new System.Diagnostics.Stopwatch();
watch.Start();
List<Image> images = new List<Image>();
Bitmap bitmap = (Bitmap)Image.FromFile(file);
int count = bitmap.GetFrameCount(FrameDimension.Page);
for (int idx = 0; idx < count; idx++)
{
// save each frame to a bytestream
bitmap.SelectActiveFrame(FrameDimension.Page, idx);
System.IO.MemoryStream byteStream = new System.IO.MemoryStream();
bitmap.Save(byteStream, ImageFormat.Tiff);
// and then create a new Image from it
images.Add(Image.FromStream(byteStream));
}
watch.Stop();
MessageBox.Show(images.Count.ToString() + "\nTime taken: " + watch.ElapsedMilliseconds.ToString());
}
Any hints or pointers on what I can do or what I should look at to speed up this process? I KNOW it can be done significantly faster - I just don't know how.
Thanks!