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);
}

始终为allFrameStream的Length=0。但是每次迭代我都可以看到ms.Length=989548。我的代码有什么错误?为什么ms.CopyTo(allFrameStream)不起作用?

最佳答案

填充后将Positionms重置为0:

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

Stream.CopyTo

关于c# - MemoryStream.CopyTo不起作用,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/22145836/

10-11 22:17
查看更多