【问题标题】:I have WPF window that i want to Print on paper size **4inch by 6inch**我有 WPF 窗口,我想在纸张尺寸上打印 **4 英寸 x 6 英寸**
【发布时间】:2013-09-26 04:56:59
【问题描述】:

我有 WPF 窗口,我想在 4 英寸 x 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");
    }

【问题讨论】:

  • 尝试设置printDlg.PrintTicket.PageMediaSize
  • @nit :但是如何以英寸为单位设置页面?我有窗口大小 Height="490" Width="410"。

标签: c# wpf printing


【解决方案1】:

在 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");
}

【讨论】:

  • 转换不适用于 Window。
  • 我给出了一般示例,您可以将 containervisual 转换为缩放。您可以在您的情况下使用 layouttransform
  • 我试过了 this.LayoutTransform= new MatrixTransform(xScale, 0, 0, yScale, xMargin, yMargin);不工作:(
  • 简洁明了,但您有点掩饰了如何获得 xMarginyMargin 值。
  • 嗨@JimMischel,这些只是您想为页面提供的水平和垂直边距。如果您希望所有边都有 1 英寸的边距,则 xMargin 和 yMargin 将为 96。
【解决方案2】:

您还可以根据纸张尺寸调整视觉效果。此代码将图像调整为 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");

【讨论】:

    猜你喜欢
    • 2018-06-24
    • 2015-07-02
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-11-13
    • 1970-01-01
    相关资源
    最近更新 更多