Can't get TIFF pages from TiffBitmapDecoder.Frames - all frames - page 1

I am trying to convert a multi-page tiff image to a multi-page XPS document. The problem I am facing is 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"));
    }
}

      

The result is an XPS with the correct page count. However, each page is a replica of the first tiff page. In fact, if I select one frame (say dec.Frames [4]) and write it to disk, it looks like the first page.

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

+1


a source to share


1 answer


Try using the following code (the commented lines are different from 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); 
}

      

+1


a source







All Articles