gpt4 book ai didi

c# - 不使用 ShowDialog 打印会出现空白页

转载 作者:太空狗 更新时间:2023-10-29 19:47:16 24 4
gpt4 key购买 nike

我在从 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");
//}
}

最佳答案

尝试在打印视觉之前执行 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");

关于c# - 不使用 ShowDialog 打印会出现空白页,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12219599/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com