我正在使用以下代碼將BMP圖像轉換爲GRF格式。將BMP圖像轉換爲GRF格式C#/ VB.NET(用於ZPL打印機)
Public Shared Function CreateGrf(filename As String, imagename As String) As String
Dim bmp As Bitmap = Nothing
Dim imgData As BitmapData = Nothing
Dim pixels As Byte()
Dim x As Integer, y As Integer, width As Integer
Dim sb As StringBuilder
Dim ptr As IntPtr
Try
bmp = New Bitmap(filename)
imgData = bmp.LockBits(New System.Drawing.Rectangle(0, 0, bmp.Width, bmp.Height), ImageLockMode.ReadWrite, bmp.PixelFormat)
width = (bmp.Width + 7) \ 8
pixels = New Byte(width - 1) {}
sb = New StringBuilder(width * bmp.Height * 2)
sb.Append(Environment.NewLine)
ptr = imgData.Scan0
For y = 0 To bmp.Height - 1
Marshal.Copy(ptr, pixels, 0, width)
For x = 0 To width - 1
sb.AppendFormat("{0:X2}", CByte(Not pixels(x)))
Next
sb.Append(Environment.NewLine)
ptr = ptr.ToInt64() + imgData.Stride
Next
Finally
If bmp IsNot Nothing Then
If imgData IsNot Nothing Then
bmp.UnlockBits(imgData)
End If
bmp.Dispose()
End If
End Try
Return [String].Format("~DG{0},{1},{2},", imagename, width * y, width) + sb.ToString()
End Function
但是有在轉換後的GRF文件的末尾繪製一個額外的垂直線,即使存在BMP文件沒有這樣的線。除此之外,尺寸和一切都是好的。看起來GRF文件中每行的最後一個像素(十六進制值)是不正確的。
原始BMP文件。
轉換GRF文件