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);
}
}
Solved.
The solution is to set a compression:
pdf.CompressionMethod = CompressionMethod.JPEG;
Has anyone an idea why?
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With