0

I have tiff file with 7000 pages, each image is 300 x 309 pixels. The entire tiff file is 2.5GB. I was able to read all the images and display in my WPF app. But when I try to create a new tiff file which is a subset of images it says 'The image data generated an overflow during processing'.

This only happens after page 5789 for some reason. If I were to write all the pages from 1 to 5788 it works. But If I were to start from 5789 it just throws this exception.

I tried checking the display settings from this answer. But it is default everywhere for me.

My code to read and write the tiff files (.Net framework 4.7.1):

Stream imageStreamSource = new FileStream(tiffFile, FileMode.Open, FileAccess.Read, FileShare.Read);
TiffBitmapDecoder decoder = new TiffBitmapDecoder(imageStreamSource, BitmapCreateOptions.None, BitmapCacheOption.Default);
var images = decoder.Frames.Cast<BitmapSource>().ToList().GetRange(5000, 1000);

using (var fs = new FileStream(saveTiffFile, FileMode.Create))
{
    BitmapEncoder encoder = new TiffBitmapEncoder();
    for (int i = 0; i < images.Count; i++)
    {
        encoder.Frames.Add(BitmapFrame.Create(images[i]));
    }
    encoder.Save(fs);
}
4
  • 1
    would 5789 pages correspond to about 2Gb? This is the max size of a byte array in .net. There might be ways around this, but I would not be shocked if TiffBitmapEncoder has such a limit. Commented Nov 27, 2024 at 15:09
  • 1
    Try using a different image library, like ImageSharp Commented Nov 27, 2024 at 15:26
  • @JonasH Tried enabling gcAllowVeryLargeObjects to true. Still facing the same issue. So ig it's not the array size issue? Commented Nov 27, 2024 at 17:54
  • 1
    I believe gcAllowVeryLargeObjects only affects the size of arrays, as in number of bytes. Not the item limit. Ofc, for byte arrays, the number of bytes and number of items is the same, so the lower limit applies. As Blindy mentions, your best bet is probably to use a different encoder. Commented Nov 28, 2024 at 7:32

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.