WPF 大多数时候不会打印

发布于 2025-01-06 06:54:06 字数 1173 浏览 0 评论 0原文

我是 WPF 的初学者,打印时遇到这个问题。我使用以下代码:

    public static void PrintImageFile(string ImagePath, int ImageCopyCount, PageOrientation ImagePageOrientation = PageOrientation.Unknown, PrintQueue Printer = null, string PrintingName = "")
    {            
        PrintDialog printDialog = SetPrintDialog(ImageCopyCount, ImagePageOrientation, Printer);
        BitmapImage imageToPrint = BitmapImageFromPath(ImagePath);
        PrintCapabilities capabilities = printDialog.PrintQueue.GetPrintCapabilities(printDialog.PrintTicket);
        Size size = new Size(capabilities.PageImageableArea.ExtentWidth, capabilities.PageImageableArea.ExtentHeight);
        DrawingVisual drawingVisualToPrint = BitmapImageToDrawingVisual(imageToPrint, size);

        try
        {
            printDialog.PrintVisual(drawingVisualToPrint, PrintingName);
        }
        catch (Exception e)
        {
            Info("Error in PrintImageFile : " + e.Message);
        }
    }

问题是大多数时候它不会打印。在这些情况下,打印作业会短暂显示在打印机的打印作业列表中,然后消失。没有任何类型的错误消息,并且程序的行为就像 PrintVisual 从未被执行过一样。 不过,大约六到七次中有 1 次打印完全按照预期进行……

否则打印机工作正常。

我花了很多时间试图理解这一点,但我还没有看到有人遇到同样的问题。 谢谢。

I'm a beginner in WPF and I have this problem with printing. I use the following code :

    public static void PrintImageFile(string ImagePath, int ImageCopyCount, PageOrientation ImagePageOrientation = PageOrientation.Unknown, PrintQueue Printer = null, string PrintingName = "")
    {            
        PrintDialog printDialog = SetPrintDialog(ImageCopyCount, ImagePageOrientation, Printer);
        BitmapImage imageToPrint = BitmapImageFromPath(ImagePath);
        PrintCapabilities capabilities = printDialog.PrintQueue.GetPrintCapabilities(printDialog.PrintTicket);
        Size size = new Size(capabilities.PageImageableArea.ExtentWidth, capabilities.PageImageableArea.ExtentHeight);
        DrawingVisual drawingVisualToPrint = BitmapImageToDrawingVisual(imageToPrint, size);

        try
        {
            printDialog.PrintVisual(drawingVisualToPrint, PrintingName);
        }
        catch (Exception e)
        {
            Info("Error in PrintImageFile : " + e.Message);
        }
    }

The problem is that it won't print most of the times. In those cases the print job is briefly displayed in the printer's list of print jobs and then disappears. There's no error message of any kind and the program behaves as if PrintVisual had never been executed.
Around 1 out of 6-7 times though, the printing occurs exactly as expected ...

The printer works fine otherwise.

I have spent a lot of time trying to understand this and I haven't seen anyone having the same issue.
Thanks.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

琉璃繁缕 2025-01-13 06:54:06

我已经找到了这种行为的原因。

这是由程序使用的清单引起的,以便以管理员身份自动执行。
不记得我从哪里得到它,但它是:

<?xml version="1.0" encoding="utf-8"?>
<asmv1:assembly manifestVersion="1.0" xmlns="urn:schemas-microsoft-com:asm.v1" xmlns:asmv1="urn:schemas-microsoft-com:asm.v1" xmlns:asmv2="urn:schemas-microsoft-com:asm.v2" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
  <assemblyIdentity version="1.0.0.0" name="MyApplication.app"/>
  <trustInfo xmlns="urn:schemas-microsoft-com:asm.v2">
    <security>
      <requestedPrivileges xmlns="urn:schemas-microsoft-com:asm.v3">

        <requestedExecutionLevel level="requireAdministrator" uiAccess="false" />
      </requestedPrivileges>
    </security>
  </trustInfo>

  <compatibility xmlns="urn:schemas-microsoft-com:compatibility.v1">
    <application>

    </application>
  </compatibility>
</asmv1:assembly>

我确信这就是造成它的原因,我做了不同的测试项目。一旦我删除项目属性中的清单(在“应用程序”下),打印就会按预期进行。

但仍然需要以管理员身份执行程序。

有谁对此类问题有经验吗?

(我在Windows 7下运行调试器)

I have found the cause of this behavior.

It is caused by the manifest the program uses in order to automatically execute as administrator.
Can’t remember where I got it from but here it is :

<?xml version="1.0" encoding="utf-8"?>
<asmv1:assembly manifestVersion="1.0" xmlns="urn:schemas-microsoft-com:asm.v1" xmlns:asmv1="urn:schemas-microsoft-com:asm.v1" xmlns:asmv2="urn:schemas-microsoft-com:asm.v2" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
  <assemblyIdentity version="1.0.0.0" name="MyApplication.app"/>
  <trustInfo xmlns="urn:schemas-microsoft-com:asm.v2">
    <security>
      <requestedPrivileges xmlns="urn:schemas-microsoft-com:asm.v3">

        <requestedExecutionLevel level="requireAdministrator" uiAccess="false" />
      </requestedPrivileges>
    </security>
  </trustInfo>

  <compatibility xmlns="urn:schemas-microsoft-com:compatibility.v1">
    <application>

    </application>
  </compatibility>
</asmv1:assembly>

I’m sure this is what’s causing it, I have made different test projects. As soon as i remove the manifest in the project properties (under Application), printing occurs as expected.

Still need to execute program as administrator though.

Anyone who has any experience with this kind of issue?

(I’m running debugger under windows 7)

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文