8

我在从 WPF 项目打印时遇到了一个奇怪的问题。我正在打印应用程序的屏幕截图以用于报告目的,并且一切正常。当前用户按下打印,打印对话框出现,他们打印出捕获的图像。

但是,我希望能够直接打印到默认打印机而不显示对话框。这应该很容易通过注释掉ShowDialog()语句并让其余的发生。打印机仍在打印,但页面始终为空白。谁能解释这种行为?

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");
    //}
}
4

1 回答 1

2

尝试在 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");
于 2012-09-14T20:59:45.950 回答