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

310 views Asked by At

I tried to print but the result of the printing status did not print immediately and the print time was approximately 10-12 seconds whether there is a solution so that it can be printed immediately or there is a need to be in the settings on my form

thanks

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
0

There are 0 answers