Impossibile recuperare le pagine TIFF da TiffBitmapDecoder.Frames - tutti i frame sono pagina 1

StackOverflow https://stackoverflow.com/questions/830386

  •  06-07-2019
  •  | 
  •  

Domanda

Sto cercando di convertire un'immagine tiff multipagina in un documento XPS multipagina. Il problema che sto riscontrando è con TiffBitmapDecoder e i suoi BitmapFrames.

Ecco il codice:

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

Ciò si traduce in un XPS con il numero corretto di pagine. Tuttavia, ogni pagina è una replica della prima pagina del tiff. In effetti, se scelgo un singolo fotogramma (diciamo, dec.Frames [4]) e lo scrivo su disco, sembra la prima pagina.

Che diamine sto facendo di sbagliato qui? I frame non sono effettivamente le singole pagine dell'immagine? Come posso farli uscire e lavorare con loro ???

È stato utile?

Soluzione

Prova a utilizzare il seguente codice (le righe commentate sono diverse dalla tua versione):

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); 
}
Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a StackOverflow
scroll top