having the following problem sending a bitarray to ESC/POS compatible printers to print some images, in this case QRCodes,
this is the image of the problem:
image with wihte spaces on the lines
this is the code:
Public Shared Function ConvertImagetoBytes(BM As Bitmap) As Byte()
Dim Data As BitMapData = GetBitmapData(BM)
Dim Op As New MemoryStream
Dim bw As New BinaryWriter(Op)
bw.Write(Chr(Keys.Escape))
bw.Write("@"c)
bw.Write(Chr(Keys.Escape))
bw.Write("3"c)
bw.Write(CByte(24))
Dim offset As Integer = 0
Dim width As Byte()
While offset < Data.Height
bw.Write(Chr(Keys.Escape))
bw.Write("*"c)
bw.Write(CByte(33))
width = BitConverter.GetBytes(Data.Width)
bw.Write(width(0))
bw.Write(width(1))
For x As Integer = 0 To Data.Width - 1
For k As Integer = 0 To 2
Dim slice As Byte = 0
For b As Integer = 0 To 7
Dim y As Integer = (((offset 8) + k) * 8) + b
Dim i As Integer = (y * Data.Width) + x
Dim v As Boolean = False
If i < Data.Dots.Length Then
v = Data.Dots(i)
End If
slice = slice Or CByte((If(v, 1, 0)) << (7 - b))
Next
bw.Write(slice)
Next
Next
offset = offset + 24
bw.Write(vbLf.ToCharArray)
End While
bw.Write(Chr(Keys.Escape))
bw.Write("2"c)
bw.Write(CByte(30))
bw.Flush()
Return Op.ToArray
End Function
this converts the image in bitmapdata to be processed in the upper function
Private Shared Function GetBitmapData(BM As Bitmap) As BitMapData
Dim threshold = 127
Dim index As Integer = 0
Dim dimensions As Integer = BM.Width * BM.Height
Dim dots As BitArray = New BitArray(dimensions)
Dim res As New BitMapData
Dim a As Integer
For y = 0 To BM.Height - 1
For x = 0 To BM.Width - 1
Dim col As Color = BM.GetPixel(x, y)
Dim luminance = CInt(col.R * 0.3 + col.G * 0.59 + col.B * 0.11)
If (luminance < threshold) = True Then
a = 1
End If
dots(index) = (luminance < threshold)
index = index + 1
Next
Next
res.Dots = dots : res.Height = BM.Height : res.Width = BM.Width
Return res
End Function
the structure of the bitmap data
Private Class BitMapData
Public Dots As BitArray
Public Height As Int16
Public Width As Int16
End Class
thanks for your help.
i have no idea why is this happening
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…