Creating multi-page TIFFs with Magick.NET

I am using Magick.NET and am trying to create multipage TIFF files. My input is a PDF file. But writing the result to a MemoryStream or getting it as a byte array results in an error:

iisexpress.exe: Error clearing data before writing directory. `TIFFWriteDirectorySec'@error/tiff.c/TIFFErrors/551

But when I write the result to a file on my hard drive, there is no error and the file is fine.

Here is my code:

var outputStream = new MemoryStream();
using (var inputPdf = new MagickImageCollection())
{
    inputPdf.Read(rawData, settings);

    using (var tif = new MagickImageCollection())
    {
        foreach (var pdf in inputPdf)
        {
            pdf.Depth = 8;
            pdf.Format = MagickFormat.Tif;
            tif.Add(pdf);
        }

        if (debug)
        {
            // Writing the data to a file is successful!
            tif.Write(pathImage);
        }

        // But writing it to a stream results in the error!
        //tif.Write(outputStream);

        // Same as getting the data as byte-array!
        var outputData = tif.ToByteArray(MagickFormat.Tif);
        outputStream.Write(outputData, 0, outputData.Length);
    }
}

      

+3


source to share


1 answer


solvable.

The solution is to set compression:



pdf.CompressionMethod = CompressionMethod.JPEG;

      

Anyone have an idea why?

+2


source







All Articles