I have a WPF window that I want to print on ** 4inch to 6inch **

I have a WPF window that I want to print on 4inch to 6inch paper .

I don’t understand where to set this size? I use the window size for printing, but the window size does not work. my printer is not a fixed paper size.
this is my print code:

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"); } 
+4
source share
2 answers

In WPF, 1 unit = 1/96 of inch , so you can calculate your size in inches using this formula

you can set printDlg.PrintTicket.PageMediaSize to the paper size, and then convert the print window in this area, as shown below:

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

You can also customize the visual format for paper size. This code adjusts the image to 4x6 inch paper.

  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"); 
0
source

All Articles