我使用 ESC/P 或 Epson Standard Code for Printers 來加粗。但我發現有一個錯誤“不支持給定路徑的格式”。有沒有最好的解決方案?
謝謝
不支持給定路徑的格式
Dim ESC As String = "\u001B"
Dim BoldOn As String = (ESC ("E" "\u0001"))
Dim BoldOff As String = (ESC ("E" "\0"))
Public Shared Function SendFileToPrinter(ByVal szPrinterName As String, ByVal szFileName As String) As Boolean
' Open the file.
Using fs As New FileStream(szFileName, FileMode.Open)
' Create a BinaryReader on the file.
Dim br As New BinaryReader(fs)
' Dim an array of bytes big enough to hold the file's contents.
Dim bytes(fs.Length - 1) As Byte
Dim bSuccess As Boolean = False
' Your unmanaged pointer.
Dim pUnmanagedBytes As New IntPtr(0)
Dim nLength As Integer
nLength = Convert.ToInt32(fs.Length)
' Read the contents of the file into the array.
bytes = br.ReadBytes(nLength)
' Allocate some unmanaged memory for those bytes.
pUnmanagedBytes = Marshal.AllocCoTaskMem(nLength)
' Copy the managed byte array into the unmanaged array.
Marshal.Copy(bytes, 0, pUnmanagedBytes, nLength)
' Send the unmanaged bytes to the printer.
bSuccess = SendBytesToPrinter(szPrinterName, pUnmanagedBytes, nLength)
' Free the unmanaged memory that you allocated earlier.
Marshal.FreeCoTaskMem(pUnmanagedBytes)
Return bSuccess
End Using
End Function
Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
Dim printer As String = "Generic / Text Only"
For i As Integer = 1 To 1
SendFileToPrinter(printer, (BoldOn ("C:\vDos\#LPT1.asc" BoldOff)))
'SendFileToPrinter(printer, "C:\vDos\#LPT1.asc") if I use this code then the error does not appear
Next i
End Sub
uj5u.com熱心網友回復:
您正在將結果傳遞(BoldOn ("C:\vDos\#LPT1.asc" BoldOff))給方法的szFileName引數,但使用該引數的唯一位置是:
Using fs As New FileStream(szFileName, FileMode.Open)
FileStream就建構式而言,您添加的前綴和后綴正在創建一個不是有效檔案路徑的值,因此出現例外。您需要將有效的檔案路徑傳遞給該建構式。
似乎該前綴和后綴應該以某種方式傳遞給列印機,但您沒有這樣做。我的猜測是您需要將該前綴和后綴添加到檔案中的實際資料,而不是檔案路徑,例如
Dim BoldOn As Byte() = Encoding.UTF8.GetBytes(ESC ("E" "\u0001"))
Dim BoldOff As Byte() = Encoding.UTF8.GetBytes(ESC ("E" "\0"))
Dim fileContents = File.ReadAllBytes(filePath)
Dim dataToPrint = BoldOn.Concat(fileContents).Concat(BoldOff).ToArray()
看來我需要詳細說明,所以這是您修改的原始代碼以包含我上面解釋的內容:
Private Shared ESC As String = "\u001B"
Private Shared BoldOn As Byte() = Encoding.UTF8.GetBytes(ESC ("E" "\u0001"))
Private Shared BoldOff As Byte() = Encoding.UTF8.GetBytes(ESC ("E" "\0"))
Public Shared Function SendFileToPrinter(printerName As String, filePath As String) As Boolean
Dim bytes = BoldOn.Concat(File.ReadAllBytes(filePath)).Concat(BoldOff).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
Dim printer = "Generic / Text Only"
SendFileToPrinter(printer, "C:\vDos\#LPT1.asc")
End Sub
請注意,您需要將這些列印機命令與檔案內容結合起來只是一個有根據的猜測。由您來確認,如果不是這樣,請找出需要什么。
轉載請註明出處,本文鏈接:https://www.uj5u.com/shujuku/512241.html
