2013-09-26 165 views
4

我有WPF窗口,我想打印纸张尺寸4英寸6英寸我有WPF窗口,我想打印在纸张尺寸** 4英寸6英寸**

我不明白在哪里设置这个尺寸? 我使用窗口大小打印,但窗口大小不起作用。 我的打印机没有固定纸张尺寸。
这是我的打印代码:

private void _print() 
    { 
     PrintDialog printDlg = new System.Windows.Controls.PrintDialog(); 
     //printDlg.ShowDialog(); 
     //get selected printer capabilities 
     System.Printing.PrintCapabilities capabilities = printDlg.PrintQueue.GetPrintCapabilities(printDlg.PrintTicket); 

     //get scale of the print wrt to screen of WPF visual 
     double scale = Math.Min(capabilities.PageImageableArea.ExtentWidth/this.ActualWidth, capabilities.PageImageableArea.ExtentHeight/
         this.ActualHeight); 

     //Transform the Visual to scale 
     this.LayoutTransform = new ScaleTransform(scale, scale); 

     //get the size of the printer page 
     Size sz = new Size(this.ActualWidth, this.ActualHeight); 

     //update the layout of the visual to the printer page size. 
     this.Measure(sz); 
     this.Arrange(new Rect(new Point(capabilities.PageImageableArea.OriginWidth, capabilities.PageImageableArea.OriginHeight), sz)); 

     //now print the visual to printer to fit on the one page. 
     printDlg.PrintVisual(this, "Print Page"); 
    } 
+1

尝试设置'printDlg.PrintTicket.PageMediaSize' – Nitin

+0

@nit:但是如何设置页面中英寸?我有窗口大小高度=“490”宽度=“410”。 –

+0

请参阅我的回答 – Nitin

回答

4

在WPF 1 unit = 1/96 of inch,这样你就可以在英寸使用这个公式

计算你的尺寸,你可以设置printDlg.PrintTicket.PageMediaSize到纸张的大小,然后你的窗口变换以在该区域打印如下:

private void _print() 
{ 
     PrintDialog printDlg = new System.Windows.Controls.PrintDialog(); 

     PrintTicket pt = printDlg.PrintTicket; 
     Double printableWidth = pt.PageMediaSize.Width.Value; 
     Double printableHeight = pt.PageMediaSize.Height.Value; 

     Double xScale = (printableWidth - xMargin * 2)/printableWidth; 
     Double yScale = (printableHeight - yMargin * 2)/printableHeight; 


     this.Transform = new MatrixTransform(xScale, 0, 0, yScale, xMargin, yMargin); 


    //now print the visual to printer to fit on the one page. 
    printDlg.PrintVisual(this, "Print Page"); 
} 
+0

变换不适用于Window。 –

+0

我给了一个通用的例子,你可以将容器视图转换成比例..你可以在你的案例中使用layouttranform – Nitin

+0

我试过这个this.LayoutTransform = new MatrixTransform(xScale,0,0,yScale,xMargin,yMargin);不工作:( –

0

您也可以根据纸张大小调整Visual。该代码将图像调整为4x6英寸的纸张。

 var photo = new BitmapImage(); 
     photo.BeginInit(); 
     photo.CacheOption = BitmapCacheOption.OnLoad; 
     photo.UriSource = new Uri(photoPath); 
     photo.EndInit(); 

     bool isPortrait = photo.Width < photo.Height; 
     if (isPortrait) 
     { 
      var transformedPhoto = new BitmapImage(); 
      transformedPhoto.BeginInit(); 
      transformedPhoto.CacheOption = BitmapCacheOption.OnLoad; 
      transformedPhoto.UriSource = new Uri(photoPath); 
      transformedPhoto.Rotation = Rotation.Rotate270; 
      transformedPhoto.EndInit(); 

      photo = transformedPhoto; 
     } 

     int width = 6; 
     int height = 4; 

     double photoScale = Math.Max(photo.Width/(96 * width), photo.Height/(96 * height)); 

     var vis = new DrawingVisual(); 
     var dc = vis.RenderOpen(); 

     dc.DrawImage(photo, new Rect 
     { 
      Width = photo.Width/photoScale, 
      Height = photo.Height/photoScale 
     }); 

     dc.Close(); 

     var pdialog = new PrintDialog(); 
     pdialog.PrintTicket.PageOrientation = PageOrientation.Landscape; 
     pdialog.PrintTicket.PageBorderless = PageBorderless.Borderless; 

     pdialog.PrintTicket.PageMediaSize = new PageMediaSize(PageMediaSizeName.NorthAmerica4x6); 
     pdialog.PrintTicket.Duplexing = Duplexing.OneSided; 
     pdialog.PrintTicket.CopyCount = 1; 
     pdialog.PrintTicket.OutputQuality = OutputQuality.Photographic; 
     pdialog.PrintTicket.PageMediaType = PageMediaType.Photographic; 

     pdialog.PrintQueue = new PrintQueue(new PrintServer(), PrinterName); 
     pdialog.PrintVisual(vis, "My Visual");