提问人:roy 提问时间:8/4/2022 最后编辑:roy 更新时间:8/4/2022 访问量:288
如何在 vb.net 中的 winform 中更快地将文件发送到打印机
how Send File To Printer faster in winform in vb.net
问:
我尝试打印,但打印状态的结果没有立即打印,打印时间约为 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
答: 暂无答案
评论
SendBytesToPrinter