Question

TiffBitmapDecoder decoder = new TiffBitmapDecoder(imageStreamSource, BitmapCreateOptions.PreservePixelFormat, BitmapCacheOption.Default);

using (MemoryStream allFrameStream = new MemoryStream())
{
    foreach (BitmapFrame frame in decoder.Frames)
    {
        using (MemoryStream ms= new MemoryStream())
        {
            JpegBitmapEncoder enc = new JpegBitmapEncoder();
            enc.Frames.Add(BitmapFrame.Create(frame));
            enc.Save(ms);
            ms.CopyTo(allFrameStream);
        }
    }

    Document documentPDF = new Document();
    PdfWriter writer = PdfWriter.GetInstance(documentPDF, allFrameStream);
}

Always allFrameStream's Length=0. But each iteration I could see ms.Length=989548. What is the error in my code. why ms.CopyTo(allFrameStream) is not working?

No correct solution

OTHER TIPS

Reset Position of ms to 0 after you fill it:

enc.Save(ms);
ms.Position = 0;
ms.CopyTo(allFrameStream);

From Stream.CopyTo

Copying begins at the current position in the current stream

Try executing allFrameStream.Position = 0; just before writing to the PDF.

After writing to ms, the position of ms is at its end. You have to seek to the beginning of the stream, e.g. with:

ms.Seek(0,System.IO.SeekOrigin.Begin);

After that ms.CopyTo is working correctly.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top