Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Creating Multipage TIFF with Magick.NET

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

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

But when I write the result to a file on the harddisk 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);
    }
}
like image 720
xforfun Avatar asked Oct 20 '22 11:10

xforfun


1 Answers

Solved.

The solution is to set a compression:

pdf.CompressionMethod = CompressionMethod.JPEG;

Has anyone an idea why?

like image 57
xforfun Avatar answered Oct 22 '22 02:10

xforfun