无法从 TiffBitmapDecoder.Frames 检索 TIFF 页 - 所有帧都是第 1 页

发布于 2024-07-19 05:15:23 字数 1390 浏览 8 评论 0原文

我正在尝试将多页 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])并将其写入磁盘,它看起来就像第一页。

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

I'm trying to convert a multipage tiff image into a multipage XPS document. The problem I'm having is with the TiffBitmapDecoder and its BitmapFrames.

Here's the code:

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

This results in an XPS with the correct number of pages. However, every page is a replica of the first page of the tiff. In fact, if I pick out a single frame (say, dec.Frames[4]) and write it to disk, it looks like the first page.

What the heck am I doing wrong here? Are the frames not actually the individual pages of the image? How can I get them out and work with them???

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

赠佳期 2024-07-26 05:15:23

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

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

Try using the following code (the commented lines are different than your version):

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); 
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文