Image 将BMP图像转换为GRF格式C#/VB.NET(用于ZPL打印机)
我使用以下代码将BMP图像转换为GRF格式Image 将BMP图像转换为GRF格式C#/VB.NET(用于ZPL打印机),image,bmp,zebra-printers,zpl,neodynamic,Image,Bmp,Zebra Printers,Zpl,Neodynamic,我使用以下代码将BMP图像转换为GRF格式 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 In
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
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
但是,即使BMP文件中没有垂直线,在转换后的GRF文件的末尾仍会绘制一条额外的垂直线。除此之外,尺寸和一切都还可以。在GRF文件中,每行的最后一个像素(十六进制值)似乎不正确
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
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
原始BMP文件
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
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
已转换的GRF文件封送处理副本(ptr,像素,0,宽度)
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
位图没有字节对齐。因此,在这种情况下,当您复制其中的数据时,它将剩余的位填充为黑色
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
位图宽154字节,创建19个完整字节和2个剩余像素。所以剩下的6个像素是黑色的
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
最后,您需要使用宽度可被8整除的位图,或者确保从位图到像素(x)的数据拷贝的末尾包含剩余的字节 Public函数将bmp2grf(文件名为字符串,图像名为字符串)转换为布尔值
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
将TI变暗为字符串
我觉得我很矮
作为对象的Dim WID
像物体一样暗高
尺寸TEM为短,BMPL为短,EFG为短,n2为串,LON为串
Dim头_名称为字符串,a为字符串,j为短,CONN为短,BASE1为短
尺寸L为字符串,TOT为字符串
将N作为对象
Dim TOT1为整数
将LL作为字节
FileOpen(1,文件名,OpenMode.Binary,1)'打开BMP文件以读取
FileGet(1,LL,1)
TI=转换为字符串(Chr(LL))
FileGet(1,LL,2)
TI+=转换为字符串(Chr(LL))
如果TI“BM”那么
FileClose()
返回错误
如果结束
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
高=N
FileGet(1,LL,27)
N=LL
FileGet(1,LL,29)
如果N 1或LL 1,则
'BMP颜色太多,仅支持单色图像
文件关闭(1)
返回错误
如果结束
TEM=Int(WID/8)
如果(WID Mod 8)为0,则
TEM+=1
如果结束
BMPL=TEM
如果(BMPL模块4)为0,则
BMPL+=(4-(BMPL模块4))
EFG=1
如果结束
n2=fileName.Substring(0,fileName.LastIndexOf(“\”,StringComparison.Ordinal)+1)+imageName+“.GRF”
FileOpen(2,n2,OpenMode.Output)'打开GRF以输出
TOT1=TEM*高:TOT=Mid(Str(TOT1),2)
如果Len(TOT)<5,则
TOT=字符串。左(“00000”,5-长度(TOT))+TOT
如果结束
LON=Mid(Str(TEM),2)
如果Len(LON)<3,则
LON=弦。左(“000”,3-弦(LON))+LON
如果结束
标题名称=图像名称
打印行(2,“~DG”&标题名称&“,”&TOT&“,”&LON&“,”)
对于i=高至1步骤-1
a=“”
对于j=1至TEM
COUN=62+(i-1)*BMPL+j
FileGet(1,LL,COUN)
L=LL
如果j=TEM且(EFG=1或(WID Mod 8)0),则
BASE1=2^((TEM*8-WID)模块8)
L=Int(L/BASE1)*BASE1+BASE1-1
如果结束
L=不是L
a+=右侧(十六进制(L),2)
下一个j
打印行(2,a)
接下来我
FileClose()
返回真值
端函数
1)删除此部分中的“7”:宽度=(bmp.width+7)\8
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) 检测位图在Mod之后的剩余值
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
if(bmp.Width % 8 > 0)
{
var remaining = bmp.Width % 8;
var newbmp = ResizeImage(bmp, bmp.Width + remaining, bmp.Height);
bmp.Dispose();
bmp = newbmp;
}
调整图像大小的逻辑
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
public static Bitmap ResizeImage(Image image, int width, int height)
{
var destRect = new Rectangle(0, 0, width, height);
var oldRect = new Rectangle(0, 0, image.Width, image.Height);
var destImage = new Bitmap(width, height);
destImage.SetResolution(image.HorizontalResolution, image.VerticalResolution);
using (var graphics = Graphics.FromImage(destImage))
{
graphics.FillRectangle(Brushes.White, destRect);
graphics.CompositingMode = CompositingMode.SourceCopy;
graphics.CompositingQuality = CompositingQuality.HighQuality;
graphics.InterpolationMode = InterpolationMode.HighQualityBicubic;
graphics.SmoothingMode = SmoothingMode.HighQuality;
graphics.PixelOffsetMode = PixelOffsetMode.HighQuality;
using (var wrapMode = new ImageAttributes())
{
wrapMode.SetWrapMode(WrapMode.TileFlipXY);
graphics.DrawImage(image, oldRect, 0, 0, image.Width, image.Height,
GraphicsUnit.Pixel, wrapMode);
}
}
return destImage;
}
这对我有用。我的图像现在不必被8整除