Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/319.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# WPF打印画布以适合页面_C#_Wpf_Canvas_Printing_Printdialog - Fatal编程技术网

C# WPF打印画布以适合页面

C# WPF打印画布以适合页面,c#,wpf,canvas,printing,printdialog,C#,Wpf,Canvas,Printing,Printdialog,我正试图在PrintDialog的帮助下将画布打印到打印机和文件上。我希望画布适合这一页。我能够使用以下代码实现它 private void Print(Visual v) { System.Windows.FrameworkElement e = v as System.Windows.FrameworkElement ; if (e == null) return; PrintDialog pd = new PrintDialog(); i

我正试图在
PrintDialog
的帮助下将画布打印到打印机和文件上。我希望画布适合这一页。我能够使用以下代码实现它

private void Print(Visual v)
{

    System.Windows.FrameworkElement e = v as System.Windows.FrameworkElement ;
    if (e == null)
        return;

    PrintDialog pd = new PrintDialog();
    if (pd.ShowDialog() == true)
    {
        //store original scale
        Transform originalScale = e.LayoutTransform;
        //get selected printer capabilities
        System.Printing.PrintCapabilities capabilities = pd.PrintQueue.GetPrintCapabilities(pd.PrintTicket);

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

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

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

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

        //now print the visual to printer to fit on the one page.
        pd.PrintVisual(v, "My Print");

        //apply the original transform.
        e.LayoutTransform = originalScale;
    }
}
上面的代码似乎按预期工作,但当我使用PDF writer将其写入PDF文件时,画布将在显示“保存”对话框时调整大小,并将其设置回正常状态。因此,UI中也出现了调整大小的情况

此画布已经是克隆的画布,如果不在UI中显示,则无法打印此画布,因为会发生一些后台操作来填充画布中的元素,只有在加载后才能启动这些操作。因此,克隆的画布显示为打印预览

是否有人知道一个好的解决方案,或者在现有解决方案的基础上进行改进以解决UI大小调整问题