Out of memory when converting multipage Tiff
Posted: 2016-07-06T02:49:50-07:00
Hi,
I'm having a problem with merging and converting multi page Tiffs. I am using the following code to merge multiple multipage Tiffs and apply adaptive thresholding to them:
It works fine for Tiffs with a small amount of pages, however when converting Tiffs with 50 pages or more images.Write() throws the following exception:
Memory allocation failed `C:\ImageMagickTest\ImageMagickOutput\merged.tiff' @ error/quantize.c/QuantizeImage/2705
at ImageMagick.MagickExceptionHelper.Check(IntPtr exception)
at ImageMagick.NativeHelper.CheckException(IntPtr exception)
at ImageMagick.MagickImageCollection.NativeMagickImageCollection.WriteFile(MagickImage image, MagickSettings settings)
at ImageMagick.MagickImageCollection.Write(String fileName)
at ImageMagickTest.ImageMagickController.applyAdaptiveThresholdingAndMergeTiffs(List`1 inputFiles, String outputFile)
When images.Write is called memory usage starts to drop, the exception is thrown when memory usage is only 120MB:
I have tried using
to limit memory usage, without succes.
I have created a test solution to simulate and recreate the problem that occcurs in my main application. To mimic the memory overhead of my main application I use an empty byte array of 100MB in the test.
Am I doing something wrong in my code, or is it not possible to convert multipage Tiffs with 50 pages or more?
I'm having a problem with merging and converting multi page Tiffs. I am using the following code to merge multiple multipage Tiffs and apply adaptive thresholding to them:
Code: Select all
public void applyAdaptiveThresholdingAndMergeTiffs(List<string> inputFiles, string outputFile)
{
using (MagickImageCollection images = new MagickImageCollection()) {
foreach (string inputFile in inputFiles) images.AddRange(inputFile);
for (int c = 0; c < images.Count; c++) images[c].AdaptiveThreshold(25, 25, -10);
images[0].CompressionMethod = CompressionMethod.Group4;
images.Write(outputFile);
}
}
Memory allocation failed `C:\ImageMagickTest\ImageMagickOutput\merged.tiff' @ error/quantize.c/QuantizeImage/2705
at ImageMagick.MagickExceptionHelper.Check(IntPtr exception)
at ImageMagick.NativeHelper.CheckException(IntPtr exception)
at ImageMagick.MagickImageCollection.NativeMagickImageCollection.WriteFile(MagickImage image, MagickSettings settings)
at ImageMagick.MagickImageCollection.Write(String fileName)
at ImageMagickTest.ImageMagickController.applyAdaptiveThresholdingAndMergeTiffs(List`1 inputFiles, String outputFile)
When images.Write is called memory usage starts to drop, the exception is thrown when memory usage is only 120MB:
I have tried using
Code: Select all
ResourceLimits.Memory = 900000000; //900000000 bytes or 900MB, just to be on the safe side of the ~1300MB limit of 32-bit .NET
to limit memory usage, without succes.
I have created a test solution to simulate and recreate the problem that occcurs in my main application. To mimic the memory overhead of my main application I use an empty byte array of 100MB in the test.
Am I doing something wrong in my code, or is it not possible to convert multipage Tiffs with 50 pages or more?