2015-07-21 177 views
2

我正在使用以下代碼將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文件中每行的最後一個像素(十六進制值)是不正確的。

Original BMP Image

原始BMP文件。

Converted GRF FIle

轉換GRF文件

回答

1
Public Function ConvertBmp2Grf(fileName As String, imageName As String) As Boolean 
    Dim TI As String 
    Dim i As Short 
    Dim WID As Object 
    Dim high As Object 
    Dim TEM As Short, BMPL As Short, EFG As Short, n2 As String, LON As String 
    Dim header_name As String, a As String, j As Short, COUN As Short, BASE1 As Short 

    Dim L As String, TOT As String 
    Dim N As Object 
    Dim TOT1 As Integer 
    Dim LL As Byte 

    FileOpen(1, fileName, OpenMode.Binary, , , 1) ' OPEN BMP FILE TO READ 
    FileGet(1, LL, 1) 
    TI = Convert.ToString(Chr(LL)) 
    FileGet(1, LL, 2) 
    TI += Convert.ToString(Chr(LL)) 

    If TI <> "BM" Then 
     FileClose() 
     Return False 
    End If 

    i = 17 
    FileGet(1, LL, i + 1) 
    N = LL * 256 
    FileGet(1, LL, i) 
    N = (N + LL) * 256 

    FileGet(1, LL, i + 3) 
    N = (N + LL) * 256 
    FileGet(1, LL, i + 2) 
    N += LL 
    WID = N 
    i = 21 
    FileGet(1, LL, i + 1) 
    N = LL * 256 
    FileGet(1, LL, i) 
    N = (N + LL) * 256 
    FileGet(1, LL, i + 3) 
    N = (N + LL) * 256 
    FileGet(1, LL, i + 2) 
    N += LL 
    high = N 
    FileGet(1, LL, 27) 
    N = LL 
    FileGet(1, LL, 29) 

    If N <> 1 Or LL <> 1 Then 
     'BMP has too many colors, only support monochrome images 
     FileClose(1) 
     Return False 
    End If 

    TEM = Int(WID/8) 
    If (WID Mod 8) <> 0 Then 
     TEM += 1 
    End If 
    BMPL = TEM 

    If (BMPL Mod 4) <> 0 Then 
     BMPL += (4 - (BMPL Mod 4)) 
     EFG = 1 
    End If 

    n2 = fileName.Substring(0, fileName.LastIndexOf("\", StringComparison.Ordinal) + 1) + imageName + ".GRF" 

    FileOpen(2, n2, OpenMode.Output) 'OPEN GRF TO OUTPUT 
    TOT1 = TEM * high : TOT = Mid(Str(TOT1), 2) 
    If Len(TOT) < 5 Then 
     TOT = Strings.Left("00000", 5 - Len(TOT)) + TOT 
    End If 

    LON = Mid(Str(TEM), 2) 

    If Len(LON) < 3 Then 
     LON = Strings.Left("000", 3 - Len(LON)) + LON 
    End If 

    header_name = imageName 
    PrintLine(2, "~DG" & header_name & "," & TOT & "," & LON & ",") 

    For i = high To 1 Step -1 
     a = "" 
     For j = 1 To TEM 
      COUN = 62 + (i - 1) * BMPL + j 
      FileGet(1, LL, COUN) 
      L = LL 

      If j = TEM And (EFG = 1 Or (WID Mod 8) <> 0) Then 
       BASE1 = 2^((TEM * 8 - WID) Mod 8) 
       L = Int(L/BASE1) * BASE1 + BASE1 - 1 
      End If 
      L = Not L 
      a += Right(Hex(L), 2) 
     Next j 
     PrintLine(2, a) 
    Next i 
    FileClose() 

    Return True 

End Function 
2

Marshal.Copy

位圖沒有被字節對準(PTR,像素,0,寬度)。因此,在這種情況下,當您複製數據時,將左側的位填充爲黑色。

位圖是154個字節寬,它創建了19個完整字節和2個剩餘像素。所以剩下的6個像素是黑色的。

最後,您需要使用寬度可以被8整除的位圖,或者確保從位圖到像素(x)的數據副本的結尾佔其餘字節。

相關問題