Convert BMP image to GRF format C# / VB.NET (To use in ZPL printer) -


i using following code convert bmp image grf format.

public shared function creategrf(filename string, imagename string) string     dim bmp bitmap = nothing     dim imgdata bitmapdata = nothing     dim pixels byte()     dim x integer, y integer, width integer     dim sb stringbuilder     dim ptr 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          y = 0 bmp.height - 1             marshal.copy(ptr, pixels, 0, width)             x = 0 width - 1                 sb.appendformat("{0:x2}", cbyte(not pixels(x)))             next             sb.append(environment.newline)             ptr = ptr.toint64() + imgdata.stride         next             if bmp isnot nothing             if imgdata isnot nothing                 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 

however there vertical line drawn @ end of converted grf file though there no such line in bmp file. other size , ok. seems last pixel (hex value) of each row not correct in grf file.

original bmp image

original bmp file.

converted grf file

converted grf file

public function convertbmp2grf(filename string, imagename string) boolean     dim ti string     dim short     dim wid object     dim high object     dim tem short, bmpl short, efg short, n2 string, lon string     dim header_name string, string, j short, coun short, base1 short      dim l string, tot string     dim n object     dim tot1 integer     dim ll byte      fileopen(1, filename, openmode.binary, , , 1)  ' open bmp file read     fileget(1, ll, 1)     ti = convert.tostring(chr(ll))     fileget(1, ll, 2)     ti += convert.tostring(chr(ll))      if ti <> "bm"         fileclose()         return false     end if      = 17     fileget(1, ll, + 1)     n = ll * 256     fileget(1, ll, i)     n = (n + ll) * 256      fileget(1, ll, + 3)     n = (n + ll) * 256     fileget(1, ll, + 2)     n += ll     wid = n     = 21     fileget(1, ll, + 1)     n = ll * 256     fileget(1, ll, i)     n = (n + ll) * 256     fileget(1, ll, + 3)     n = (n + ll) * 256     fileget(1, ll, + 2)     n += ll     high = n     fileget(1, ll, 27)     n = ll     fileget(1, ll, 29)      if n <> 1 or ll <> 1         'bmp has many colors, support monochrome images         fileclose(1)         return false     end if      tem = int(wid / 8)     if (wid mod 8) <> 0         tem += 1     end if     bmpl = tem      if (bmpl mod 4) <> 0         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 output     tot1 = tem * high : tot = mid(str(tot1), 2)     if len(tot) < 5         tot = strings.left("00000", 5 - len(tot)) + tot     end if      lon = mid(str(tem), 2)      if len(lon) < 3         lon = strings.left("000", 3 - len(lon)) + lon     end if      header_name = imagename     printline(2, "~dg" & header_name & "," & tot & "," & lon & ",")      = high 1 step -1         = ""         j = 1 tem             coun = 62 + (i - 1) * bmpl + j             fileget(1, ll, coun)             l = ll              if j = tem , (efg = 1 or (wid mod 8) <> 0)                 base1 = 2 ^ ((tem * 8 - wid) mod 8)                 l = int(l / base1) * base1 + base1 - 1             end if             l = not l             += right(hex(l), 2)         next j         printline(2, a)     next     fileclose()      return true  end function 

Comments