【问题标题】:Printing scales my WPF-GroupBox打印缩放我的 WPF-GroupBox
【发布时间】:2013-04-05 12:06:18
【问题描述】:

我正在尝试从我的 wpf 应用程序中打印出一些信息。我找到了一些代码来制作我想要打印的内容以适合一页并且它可以很好地完成这项工作。问题是,在我打印出我想要的东西之后,该方法缩小了我的 wpf 控件,它是一个带有图表的组框。如何将 groupbox 的大小缩放回缩放之前的大小?

private void PrintUT_Click(object sender, RoutedEventArgs e)
    {
        PrintDialog printDlg = new System.Windows.Controls.PrintDialog();
        if (printDlg.ShowDialog() == true)
        {
            //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.GBProsjektTimer.ActualWidth, capabilities.PageImageableArea.ExtentHeight /
                           this.GBProsjektTimer.ActualHeight);

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

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

            //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.GBProsjektTimer, "First Fit to Page WPF Print");
        }
    }

【问题讨论】:

    标签: c# wpf printing groupbox


    【解决方案1】:

    找到答案了!只需将 objecToPrint 替换为您的对象即可。在我的情况下是this.GBProsjektTimer.Width = double.Nan

                        objectToPrint.Width = double.NaN;
                    objectToPrint.UpdateLayout();
                    objectToPrint.LayoutTransform = new ScaleTransform(1, 1);
                    Size size = new Size(capabilities.PageImageableArea.ExtentWidth, 
                                         capabilities.PageImageableArea.ExtentHeight);
                    objectToPrint.Measure(size);
                    objectToPrint.Arrange(new Rect(new Point(capabilities.PageImageableArea.OriginWidth, 
                                          capabilities.PageImageableArea.OriginHeight), size));
    

    【讨论】:

    • 感谢您花时间发布您自己的答案。请在您能够接受时接受它——这会让其他人知道已找到解决方案。感谢并欢迎来到 SO。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-11-17
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-06-11
    相关资源
    最近更新 更多