如何在 vb.net 中的 winform 中更快地将文件发送到打印机

how Send File To Printer faster in winform in vb.net

提问人:roy 提问时间:8/4/2022 最后编辑:roy 更新时间:8/4/2022 访问量:288

问:

我尝试打印,但打印状态的结果没有立即打印,打印时间约为 10-12 秒,是否有解决方案可以立即打印,或者需要在我的表格上的设置中

谢谢

Public Shared Function SendBytesToPrinter(ByVal szPrinterName As String, ByVal pBytes As IntPtr, ByVal dwCount As Int32) As Boolean
        Dim dwError As Int32 = 0, dwWritten As Int32 = 0
        Dim hPrinter As New IntPtr(0)
        Dim di As New DOCINFOA()
        Dim bSuccess As Boolean = False ' Assume failure unless you specifically succeed.
        di.pDocName = "OUTPUTPRN"
        di.pDataType = "RAW"

        ' Open the printer.
        If OpenPrinter(szPrinterName.Normalize(), hPrinter, IntPtr.Zero) Then
            ' Start a document.
            If StartDocPrinter(hPrinter, 1, di) Then
                ' Start a page.
                If StartPagePrinter(hPrinter) Then
                    ' Write your bytes.
                    bSuccess = WritePrinter(hPrinter, pBytes, dwCount, dwWritten)
                    EndPagePrinter(hPrinter)
                End If
                EndDocPrinter(hPrinter)
            End If
            ClosePrinter(hPrinter)
        End If
        ' If you did not succeed, GetLastError may give more information
        ' about why not.
        If bSuccess = False Then
            dwError = Marshal.GetLastWin32Error()
        End If
        Return bSuccess
    End Function
Public Shared Function SendFileToPrinter(ByVal printerName As String, ByVal filePath As String) As Boolean
        Dim bytes = File.ReadAllBytes(filePath).ToArray
        Dim byteCount = bytes.Length
        Dim unmanagedBytesPointer = Marshal.AllocCoTaskMem(byteCount)

        Marshal.Copy(bytes, 0, unmanagedBytesPointer, byteCount)

        Dim success = SendBytesToPrinter(printerName, unmanagedBytesPointer, byteCount)

        Marshal.FreeCoTaskMem(unmanagedBytesPointer)

        Return success
    End Function
 Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        If My.Settings.close = True Then
Dim printer As String = "EPSON LX-300+II ESC/P"
Dim path As String = "C:\vDos\#LPT1.asc"
            For i As Integer = 1 To 1
                SendFileToPrinter(printer, path)
            Next i
            Me.Close()
        End If
    End Sub
vb.net winforms visual-studio-2010 winapi 打印后台处理程序

评论

1赞 IInspectable 8/4/2022
由于我们不知道你将不得不自己分析你的代码,以了解它把时间花在哪里。SendBytesToPrinter
3赞 Joel Coehoorn 8/4/2022
这取决于打印机在操作系统中的配置方式,而不是您的代码。如果打印机设置为“在最后一页后台打印后开始打印”,您将等待,并且任何代码更改都无济于事。如果打印机设置为“立即开始打印”或“直接打印到打印机”,则通常情况良好。收据打印机和其他进纸打印机几乎应始终使用“直接打印到打印机”。
0赞 roy 8/4/2022
@IInspectable,我已经更新了我的代码,也许你可以帮我
0赞 roy 8/4/2022
@JoelCoehoorn,表单加载可以设置这么快吗?或需要修改或添加代码
1赞 Hel O'Ween 8/4/2022
在未咨询系统管理员的情况下,不应应用程序中更改此打印后台处理程序设置。这是操作系统范围的设置,即它会影响该机器的所有打印。除了可能出于某些原因导致其按原样进行配置之外,未经用户同意,应用程序绝不应更改系统设置。

答: 暂无答案