I'm trying to print out some information from my wpf-application. I found some code to make what I want to print to fit one page and it does the job very well. The problem is that after I print what i want, the method downscale my wpf-control, which is a groupbox with a chart in it. How do i scale the size of the groupbox back to what it was before the scaling?

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");
        }
    }
有帮助吗?

解决方案

Found the answer! Just replace objecToPrint with your object. In my case that would be 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));
许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top