【问题标题】:Unable to retrieve TIFF pages from TiffBitmapDecoder.Frames -- all frames are page 1无法从 TiffBitmapDecoder.Frames 检索 TIFF 页面——所有帧都是第 1 页
【发布时间】:2010-10-24 05:49:36
【问题描述】:

我正在尝试将多页 tiff 图像转换为多页 XPS 文档。我遇到的问题是 TiffBitmapDecoder 及其 BitmapFrames。

代码如下:

private static void ToXpsDocument(string imageName, string xpsName)
{
    using (var p = Package.Open(xpsName))
    {
        PackageStore.AddPackage(new Uri("pack://thedocloljk.xps"), p);
        XpsDocument doc = new XpsDocument(p);
        var writer = XpsDocument.CreateXpsDocumentWriter(doc);
        var dec = new TiffBitmapDecoder
                          (new Uri(imageName),
                          BitmapCreateOptions.IgnoreImageCache,
                          BitmapCacheOption.None);

        var fd = new FixedDocument();
        foreach (var frame in dec.Frames)
        {
            var image = new System.Windows.Controls.Image();
            image.Source = frame;
            var fp = new FixedPage();
            fp.Children.Add(image);
            fp.Width = frame.Width;
            fp.Height = frame.Height;
            var pc = new PageContent();
            (pc as IAddChild).AddChild(fp);
            fd.Pages.Add(pc);
        }
        writer.Write(fd);
        p.Flush();
        p.Close();
        PackageStore.RemovePackage(new Uri("pack://thedocloljk.xps"));
    }
}

这会生成具有正确页数的 XPS。但是,每一页都是 tiff 第一页的副本。事实上,如果我选择一个单独的帧(比如 dec.Frames[4])并将其写入磁盘,它看起来就像是第一页。

我到底做错了什么?框架实际上不是图像的各个页面吗?我怎样才能让他们出去和他们一起工作???

【问题讨论】:

    标签: wpf tiff xps bitmapsource


    【解决方案1】:

    尝试使用以下代码(注释行与您的版本不同):

    foreach (var frameSource in dec.Frames) // note this line
    {
        var frame = BitmapFrame.Create(frameSource); // and this line
        var image = new System.Windows.Controls.Image(); 
        image.Source = frame; 
        var fp = new FixedPage(); 
        fp.Children.Add(image); 
        fp.Width = frame.Width; 
        fp.Height = frame.Height; 
        var pc = new PageContent(); 
        (pc as IAddChild).AddChild(fp); 
        fd.Pages.Add(pc); 
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2017-04-08
      • 2021-08-21
      • 1970-01-01
      • 1970-01-01
      • 2015-04-15
      • 1970-01-01
      • 2021-09-01
      • 2015-04-17
      相关资源
      最近更新 更多