我搜索了如何打印WPF控件的选项,并找到了一些解决方案.我需要将我的打印控件适合打印页面,同时保留纵横比(我的控件是方形;数独网格).
我找到了一个解决方案,它调整大小并重新定位控件以适应页面.这很好,但它也重新定位了我的窗口控制.
这是我用于打印和缩放的代码:
//get selected printer capabilities
System.Printing.PrintCapabilities capabilities = dialog.PrintQueue.GetPrintCapabilities(dialog.PrintTicket);
//get scale of the print wrt to screen of WPF visual
double scale = Math.Min(capabilities.PageImageableArea.ExtentWidth / mrizka.ActualWidth, capabilities.PageImageableArea.ExtentHeight / mrizka.ActualHeight);
//Transform the Visual to scale
mrizka.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.
mrizka.Measure(sz);
mrizka.Arrange(new Rect(new Point(capabilities.PageImageableArea.OriginWidth, capabilities.PageImageableArea.OriginHeight), sz));
dialog.PrintVisual(mrizka, mrizka.getID().ToString());
Run Code Online (Sandbox Code Playgroud)
我尝试了两个方法来解决这个问题:
克隆我的控制,然后转换克隆的一个,不影响原始.没有工作,由于某种原因我以异常结束:提供的DependencyObject不是这个Freezable的上下文,但奇怪的是仅在某些情况下.
恢复大小和位置更改.我尝试调用InvalidateArrange()方法,这似乎有效,但只在第一次调用print方法时才有效.在第二次通话期间,它没有工作.
我应该做什么,任何想法<谢谢你.
sea*_*nzi 24
我知道这个问题很老,但我自己也在寻找解决这个问题的方法.这是我目前使用的解决方案.我将原始转换存储在框架元素中,然后在打印完成后重新应用它.
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;
}
}
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
15325 次 |
| 最近记录: |