2015-05-29 36 views
6

首先,我想指出,我已經提出這是微軟的一個錯誤,但他們不願意在這個時候修正它。我正在尋找的是解決方案或更好的方式來實現我所要做的事情,因爲我們的客戶認爲這是一個相當重要的問題。在XPS文件中打印的重複圖像

代碼

MainWindow.xaml

<Grid x:Name="mainGrid"> 
    <Grid.RowDefinitions> 
     <RowDefinition Height="Auto"/> 
     <RowDefinition Height="Auto"/> 
     <RowDefinition Height="Auto"/> 
    </Grid.RowDefinitions> 
    <ListBox ItemsSource="{Binding Images}"> 
     <ListBox.ItemTemplate> 
      <DataTemplate> 
       <Image Source="{Binding}"/> 
      </DataTemplate> 
     </ListBox.ItemTemplate> 
    </ListBox> 

    <Button Content="Print to file" Grid.Row="1" Click="PrintToFile_Click"/> 
    <Button Content="Print to device" Grid.Row="2" Click="PrintToDevice_Click"/> 
</Grid> 

MainWindow.xaml.cs

public partial class MainWindow : Window 
{ 
    public IList<byte[]> Images { get; set; } 

    public MainWindow() 
    { 
     InitializeComponent(); 

     Assembly currentAssembly = Assembly.GetExecutingAssembly(); 

     this.Images = new List<byte[]> 
     { 
      ReadToEnd(currentAssembly.GetManifestResourceStream("PrintingInvestigation.Images.Chrysanthemum.jpg")), 
      ReadToEnd(currentAssembly.GetManifestResourceStream("PrintingInvestigation.Images.Desert.jpg")), 
      ReadToEnd(currentAssembly.GetManifestResourceStream("PrintingInvestigation.Images.Hydrangeas.jpg")), 
     }; 

     this.DataContext = this; 
    } 

    public static byte[] ReadToEnd(System.IO.Stream stream) 
    { 
     long originalPosition = 0; 

     if (stream.CanSeek) 
     { 
      originalPosition = stream.Position; 
      stream.Position = 0; 
     } 

     try 
     { 
      byte[] readBuffer = new byte[4096]; 

      int totalBytesRead = 0; 
      int bytesRead; 

      while ((bytesRead = stream.Read(readBuffer, totalBytesRead, readBuffer.Length - totalBytesRead)) > 0) 
      { 
       totalBytesRead += bytesRead; 

       if (totalBytesRead == readBuffer.Length) 
       { 
        int nextByte = stream.ReadByte(); 
        if (nextByte != -1) 
        { 
         byte[] temp = new byte[readBuffer.Length * 2]; 
         Buffer.BlockCopy(readBuffer, 0, temp, 0, readBuffer.Length); 
         Buffer.SetByte(temp, totalBytesRead, (byte)nextByte); 
         readBuffer = temp; 
         totalBytesRead++; 
        } 
       } 
      } 

      byte[] buffer = readBuffer; 
      if (readBuffer.Length != totalBytesRead) 
      { 
       buffer = new byte[totalBytesRead]; 
       Buffer.BlockCopy(readBuffer, 0, buffer, 0, totalBytesRead); 
      } 
      return buffer; 
     } 
     finally 
     { 
      if (stream.CanSeek) 
      { 
       stream.Position = originalPosition; 
      } 
     } 
    } 

    private void PrintToDevice_Click(object sender, RoutedEventArgs e) 
    { 
     PrintDialog dialog = new PrintDialog(); 
     if (dialog.ShowDialog() == true) 
     { 
      Thickness pageMargins; 

      if (dialog.PrintTicket.PageBorderless.HasValue == true) 
      { 
       if (dialog.PrintTicket.PageBorderless.Value == PageBorderless.Borderless) 
       { 
        pageMargins = new Thickness(0, 0, 0, 0); 
       } 
       else 
       { 
        pageMargins = new Thickness(20, 20, 20, 20); 
       } 
      } 
      else 
      { 
       pageMargins = new Thickness(20, 20, 20, 20); 
      } 

      int dpiX = 300; 
      int dpiY = 300; 
      if (dialog.PrintTicket.PageResolution != null && 
       dialog.PrintTicket.PageResolution.X.HasValue && 
       dialog.PrintTicket.PageResolution.Y.HasValue) 
      { 
       dpiX = dialog.PrintTicket.PageResolution.X.Value; 
       dpiY = dialog.PrintTicket.PageResolution.Y.Value; 
      } 
      else 
      { 
       dialog.PrintTicket.PageResolution = new PageResolution(dpiX, dpiY); 
      } 

      VisualDocumentPaginator paginator = new VisualDocumentPaginator(this.mainGrid, this.mainGrid.ActualWidth); 
      paginator.PageSize = new Size(dialog.PrintableAreaWidth, dialog.PrintableAreaHeight); 

      dialog.PrintDocument(paginator, "My first print"); 
      GC.Collect(); 
     } 
    } 

    private void PrintToFile_Click(object sender, RoutedEventArgs e) 
    { 
     string filePath = this.PrintToFile(null, this.mainGrid, "My first print", this.mainGrid.ActualHeight, this.mainGrid.ActualWidth); 

     Process.Start(filePath); 
    } 

    public string PrintToFile(Visual titleVisual, Visual contentVisual, string title, double bottomMost, double rightMost) 
    { 
     string printedFilePath = System.IO.Path.Combine(System.IO.Path.GetTempPath(), string.Format(CultureInfo.InvariantCulture, "{0}.xps", title)); 

     XpsDocument printedDocument = new XpsDocument(printedFilePath, FileAccess.Write, System.IO.Packaging.CompressionOption.SuperFast); 

     VisualDocumentPaginator paginator = new VisualDocumentPaginator(contentVisual as FrameworkElement, rightMost); 
     paginator.PageSize = new Size(793.7, 1122.5); 

     XpsDocumentWriter writer = XpsDocument.CreateXpsDocumentWriter(printedDocument); 
     writer.Write(paginator, new PrintTicket 
     { 
      Collation = Collation.Collated, 
      CopyCount = 1, 
      DeviceFontSubstitution = DeviceFontSubstitution.On, 
      Duplexing = Duplexing.OneSided, 
      InputBin = InputBin.AutoSelect, 
      OutputColor = OutputColor.Color, 
      OutputQuality = OutputQuality.High, 
      PageMediaSize = new PageMediaSize(PageMediaSizeName.ISOA4), 
      PageOrientation = PageOrientation.Portrait, 
      PageResolution = new PageResolution(PageQualitativeResolution.High), 
      PagesPerSheet = 1, 
      TrueTypeFontMode = TrueTypeFontMode.Automatic 
     }); 

     printedDocument.Close(); 

     return printedFilePath; 
    } 
} 

VisualDocumentPaginator.cs

public class VisualDocumentPaginator : DocumentPaginator 
{ 
    #region Fields 

    private double desiredWidth; 
    private FrameworkElement element; 

    #endregion 

    #region Properties 

    public int Columns 
    { 
     get 
     { 
      return 1;// (int)Math.Ceiling(Element.ActualWidth/PageSize.Width); 
     } 
    } 

    public int Rows 
    { 
     get 
     { 
      return (int)Math.Ceiling(element.ActualHeight/PageSize.Height); 
     } 
    } 

    #endregion 

    #region Constructors 

    public VisualDocumentPaginator(FrameworkElement element, double desiredWidth) 
    { 
     this.desiredWidth = desiredWidth; 
     this.element = element; 
    } 

    #endregion 

    #region DocumentPaginator Members 

    public override DocumentPage GetPage(int pageNumber) 
    { 
     TransformGroup transforms = new TransformGroup(); 

     double scaleRatio = this.PageSize.Width/this.desiredWidth; 
     int row = (pageNumber/Columns); 

     double pageHeight = -PageSize.Height * row/scaleRatio; 
     double pageWidth = -PageSize.Width * (pageNumber % Columns); 

     transforms.Children.Add(new TranslateTransform(pageWidth, pageHeight)); 

     // Make sure the control is stretched to fit the page size. 
     if (scaleRatio != double.NaN) 
     { 
      ScaleTransform st = new ScaleTransform(scaleRatio, scaleRatio); 
      transforms.Children.Add(st); 
     } 

     element.RenderTransform = transforms; 

     Size elementSize = new Size(this.desiredWidth, element.ActualHeight); 
     element.Measure(elementSize); 
     element.Arrange(new Rect(new Point(0, 0), elementSize)); 

     var page = new DocumentPage(element, this.PageSize, new Rect(), new Rect()); 
     element.RenderTransform = null; 

     return page; 
    } 

    public override bool IsPageCountValid 
    { 
     get { return true; } 
    } 

    public override int PageCount 
    { 
     get 
     { 
      return Columns * Rows; 
     } 
    } 

    public override Size PageSize { set; get; } 

    public override IDocumentPaginatorSource Source 
    { 
     get { return null; } 
    } 

    #endregion 
} 

抱歉發佈所有的代碼,但它涵蓋了所有我看到問題的領域。如果這裏有幫助的是Microsoft bug report,它附有一個示例項目,其中可以複製問題。

問題
問題寫入,其中僅所述第一圖像被打印3次的XPS文件時,如果點擊了「打印到設備」按鈕時,則正確的圖像被打印僅出現。

我綁定到byte []的原因是因爲我在本地SQL CE數據庫中保存了我的圖像。我們將它們存儲在一個數據庫中,因爲它們只有很小的~2KB,並且我們允許用戶將自己的圖標導入到系統中以供使用,並且我們希望有一種機制來保證它們不會被意外刪除。

注意
我注意到,如果我不綁定到字節[]上面提到的話,我看不出問題。鑑於系統目前的工作方式是將圖像存儲在數據庫中,如果有解決方法,我寧願堅持使用它,但我並不完全反對替換這些圖像的存儲機制。

回答

1

我爲構建於WPF上的文檔管理系統構建了自定義打印解決方案。我開始使用System.Printing命名空間,但在.NET中發現了很長時間內微軟尚未解決的無窮無盡的錯誤。沒有可能的解決方法,我最終使用了爲Windows Forms構建的更成熟的System.Drawing.Printing命名空間,我發現它沒有問題。

這可能需要一段時間才能將代碼重寫爲System.Drawing.Printing,但是您不太可能在此過程中撞到某個磚牆。

+0

感謝您的支持。我希望能夠找到解決問題的相當快速的解決方案,但客戶現在已決定放棄XPS支持並轉向PDF,因此我認爲我們將重寫我們使用的打印邏輯。我一定會考慮System.Drawing.Printing。 – Bijington

1

我遇到過類似的問題,其中第一張圖像被複制並替換了所有其他圖像。就我而言,打印到設備,XPS文檔或PDF文檔並不重要,問題仍然存在。

分析

我已經使用了.NET程序集反編譯找出與圖像System.Windows.Xps.XpsDocumentWriter類交易如何找出如果問題是在我的代碼或框架的代碼。我發現框架使用dictionnairies將資源(如圖像)導入到文檔中。即使圖像僅在XPS文檔中導入一次,文檔也可以多次引用它們。

就我而言,我能夠發現問題出在System.Windows.Xps.Serialization.ImageSourceTypeConverter.GetBitmapSourceFromImageTable方法中。當圖像從System.Windows.Media.Imaging.BitmapFrame構建時,轉換器將使用一個鍵在其字典之一中查找資源。在這種情況下,密鑰對應於由BitmapFrame.Decoder.ToString()方法返回的字符串的哈希碼。不幸的是,由於我的圖像是從字節數組而不是URI構建的,解碼器的ToString方法返回「image」。由於該字符串將始終生成相同的哈希碼,無論圖像如何,ImageSourceTypeConverter將認爲所有圖像都已添加到XPS文檔,並將返回要使用的第一張和唯一圖像的Uri。這解釋了爲什麼第一張圖像是重複的並正在替換所有其他圖像。

嘗試

我要解決這一問題的首次嘗試是覆蓋System.Windows.Media.Imaging.BitmapDecoder.ToString()方法。爲了這樣做,我試圖將BitmapFrameBitmapDecoder包裝到我自己的BitmapFrameBitmapDecoder中。不幸的是,BitmapDecoder類包含我無法定義的internal abstract方法。由於我無法創建自己的BitmapDecoder,因此無法實施該解決方法。

解決方法

如先前mentionned,所述System.Windows.Xps.Serialization.ImageSourceTypeConverter.GetBitmapSourceFromImageTable方法會尋找一個散列碼在字典中時BitmapSourceBitmapFrame。當它不是BitmapFrame時,它將生成基於圖像二進制數據的CRC值,並在另一個字典中查找它。

在我的情況,我決定包從由System.Windows.Media.ImageSourceConverter字節數組生成成另一種類型的如System.Windows.Media.Imaging.CachedBitmapBitmapSourceBitmapFrame。因爲我沒有真正想要緩存的位圖,我創建了CachedBitmap將下列選項:

var imageSource = new CachedBitmap(bitmapFrame, BitmapCreateOptions.None, BitmapCacheOption.None); 

使用這些選項中,CachedBitmap主要是一個簡單的BitmapSource包裝。就我而言,這個解決方法解決了我的問題。