简体   繁体   English

不使用ShowDialog进行打印将显示空白页

[英]Printing without ShowDialog gives blank pages

I am experiencing an odd issue with printing from a WPF project. 我在WPF项目中打印时遇到了一个奇怪的问题。 I'm printing a screen capture of the application for reporting purposes, and all that works just fine. 我正在打印该应用程序的屏幕截图以用于报告目的,并且一切正常。 Currently the user presses print, the print dialog appears, and they print out the capture image. 当前,用户按下打印,出现打印对话框,然后打印出捕获的图像。

However, I want to be able to print directly to the default printer without showing the dialog box. 但是,我希望能够直接打印到默认打印机而不显示对话框。 This should be easily done by commenting out the ShowDialog() statement and allowing the rest to just happen. 通过注释掉ShowDialog()语句并允许其余的事情发生,可以很容易地做到这一点。 The printer still prints, but the pages are always blank. 打印机仍然可以打印,但是页面始终为空白。 Can anyone explain this behavior? 谁能解释这种行为?

private void PrintCurrentScreen()
{
    PrintDialog PD = new PrintDialog();
    PD.PrintTicket.OutputColor = OutputColor.Grayscale;
    PD.PrintTicket.OutputQuality = OutputQuality.Draft;

    PrintTicket PT = new PrintTicket();
    PT.PageOrientation = PageOrientation.Landscape;
    PT.CopyCount = 1;
    PT.PageBorderless = System.Printing.PageBorderless.Borderless;

    //---Blank pages print when commented out---//
    //if (PD.ShowDialog() == true)
    //{
    PD.PrintTicket = PT;

    DrawingVisual DV = new DrawingVisual();
    DV.Offset = new Vector(20, 20);
    DrawingContext DC = DV.RenderOpen();
    DC.DrawImage(previewimage.Source, new Rect(new Size(PD.PrintableAreaWidth - 40, PD.PrintableAreaHeight - 40)));
    DC.Close();

    PD.PrintVisual(DV, "TEST");
    //}
}

Try doing a Measure, Arrange, and UpdateLayout right before the printvisual, like this: 尝试在printvisual之前进行Measure,Arrange和UpdateLayout,如下所示:

DV.Measure(new System.Windows.Size(PD.PrintableAreaWidth,
              PD.PrintableAreaHeight));
DV.Arrange(new System.Windows.Rect(new System.Windows.Point(0, 0),
              DV.DesiredSize));

DV.UpdateLayout();
PD.PrintVisual(DV, "TEST");

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM