2009-05-06 50 views
1

我試圖將多頁TIFF圖像轉換爲多頁XPS文檔。我遇到的問題是TiffBitmapDecoder及其BitmapFrames。無法從TiffBitmapDecoder.Frames檢索TIFF頁面 - 所有框架都是第1頁

下面的代碼:

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])並將其寫入磁盤,它看起來就像是第一頁。

我在這裏做錯了什麼?這些幀實際上不是圖像的單個頁面嗎?我怎樣才能讓他們和他們一起工作?

回答

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

將檢查了這一點。 – Will 2009-06-05 14:27:50