2016-02-26 66 views
1

有什麼方法可以打印wpf窗口的屏幕截圖嗎?C#WPF - 打印WPF窗口的ScreenCapture

我嘗試了很多東西,但是它對A4尺寸來說太大了,或者因爲沒有設置邊距而裁剪掉了。 這裏的代碼我想:

樣本1:

private void btnPrintStanzbild_Click(object sender, RoutedEventArgs e) 
    { 
     Visual target = gRight; 

     Rect bounds = VisualTreeHelper.GetDescendantBounds(target); 

     RenderTargetBitmap renderTarget = new RenderTargetBitmap((Int32)bounds.Width, (Int32)bounds.Height, 96, 96, PixelFormats.Pbgra32); 

     DrawingVisual visual = new DrawingVisual(); 

     using (DrawingContext context = visual.RenderOpen()) 
     { 
      VisualBrush visualBrush = new VisualBrush(target); 
      context.DrawRectangle(visualBrush, null, new Rect(new Point(), bounds.Size)); 
     } 

     renderTarget.Render(visual); 
     PngBitmapEncoder bitmapEncoder = new PngBitmapEncoder(); 

     BitmapImage bi = new BitmapImage(); 


     bitmapEncoder.Frames.Add(BitmapFrame.Create(renderTarget)); 
     using (MemoryStream stm = new MemoryStream()) 
     { 
      bitmapEncoder.Save(stm); 
      bi.BeginInit(); 
      bi.CacheOption = BitmapCacheOption.OnLoad; 
      bi.StreamSource = stm; 
      bi.EndInit(); 
     } 



     var vis = new DrawingVisual(); 
     var dc = vis.RenderOpen(); 
     dc.DrawImage(bi, new Rect { Width = bi.Width, Height = bi.Height }); 
     dc.Close(); 

     var pdialog = new PrintDialog(); 
     if (pdialog.ShowDialog() == true) 
     { 
      var queue = pdialog.PrintQueue; 


      var area = queue.GetPrintCapabilities(pdialog.PrintTicket) 
          .PageImageableArea; 

      pdialog.PrintVisual(vis, "My Image"); 
     } 
    } 

樣品2:

PrintDialog pdialog = new PrintDialog(); 
     if (pdialog.ShowDialog() == true) 
     { 
      var queue = pdialog.PrintQueue; 


      var area = queue.GetPrintCapabilities(pdialog.PrintTicket) 
          .PageImageableArea; 

      pdialog.PrintVisual(this, "My Image"); 
     } 

示例3:

Link to sample 3

+0

所以,你要擴大在W降低到A4尺寸並打印出來? – Sakura

回答

0
private void Print() 
{ 
    PrintDialog pdialog = new PrintDialog(); 
    if (pdialog.ShowDialog() == true) 
    { 
     // Save current layout 
     Transform origTransform = LayoutTransform; 
     Size oldWindowSize = new Size(ActualWidth, ActualHeight); 

     // Get printer caps 
     PrintCapabilities capabilities = pdialog.PrintQueue.GetPrintCapabilities(pdialog.PrintTicket); 

     // Get size of the printer page 
     var sz = new Size(capabilities.PageImageableArea.ExtentWidth, capabilities.PageImageableArea.ExtentHeight); 

     // Calculate zoom level of window 
     double ratio = Math.Min(sz.Width/ActualWidth, sz.Height/ActualHeight); 

     //if (ratio < 1) // Uncomment this line if you dont want zoom out small window 
     { 
      LayoutTransform = new ScaleTransform(ratio, ratio); 
     } 

     Measure(sz); 
     Arrange(new Rect(new Point(0,0), sz)); 
     pdialog.PrintVisual(this, "My Image"); 

     // Store old layout 
     LayoutTransform = origTransform; 
     Measure(oldWindowSize); 
     Arrange(new Rect(new Point(0, 0), oldWindowSize)); 
    } 
}