Convert image to byte array - what am I doing wrong.

Post

Posted
Rating:
#1 (In Topic #1176)
Enthusiast
AndyGable is in the usergroup ‘Enthusiast’
Hi everyone

I need someone to look over this code for me and to tell me what I am doing wrong

I have a imageview on the form

Code (gambas)

  1. Public Sub ConvertImage()
  2.     Dim bmpFilePath As String = "till_logo.bmp"
  3.     Dim byteArray As Byte[]
  4.    
  5.     ' Load the BMP image into the Image view
  6.     FMain.ImageView1.Image = Image.Load(bmpFilePath)
  7.    
  8.     ' Convert the BMP image to a byte array
  9.     byteArray = ImageToByteArray(FMain.ImageView1.Image)
  10.    
  11.    
  12.    
  13.     ' Now 'byteArray' contains the byte representation of the BMP image
  14.  
  15. Private Function ImageToByteArray(image As Image) As Byte[]
  16.    
  17.     ' Create a memory stream to store the image data
  18.     stream = Open Memory image For Read Write
  19.  
  20.     ' Save the image to the memory stream
  21.     image.Save(stream)
  22.    
  23.     ' Seek to the beginning of the memory stream
  24.     stream.seek">Seek(0)
  25.    
  26.     ' Read the byte array from the memory stream
  27.     Return ImageToByteArray = stream.Read(stream.Length)
  28.    
  29.    ' Close the memory stream
  30.  
  31.  

It errors out at the stream = Open Memory image For Read Write in the Private Function ImageToByteArray function

The Error Message is "Type mismatch: Wanted pointer, got Image instead (ImageControl:22)


Anyone have any idea's?
Online now: No Back to the top

Post

Posted
Rating:
#2
Regular
vuott is in the usergroup ‘Regular’
The "Memory Stream " resource is used to read, but–specifically–to write data a reserved memory area, pointed to by a "Pointer ".
Therefore, the "Memory Stream " resource must be passed the ".Data" Property of the "Image " Class, as that one returns precisely the memory address (i.e., a "Pointer "), where the data (…pixels) of the "Image " Object are stored.
Since you only need to read into the memory area, whose address is returned by the ".Data" Property of the "Image" Class, you can also more simply use Gambas "Byte@()" dereferencing function.

/lang/memory - Gambas Documentation
/lang/byte@ - Gambas Documentation
Definizione ed uso dei Memory Stream - Gambas-it.org - Wikipedia
Dereferenziare un Puntatore con le funzioni specifiche di dereferenziazione e con i Memory Stream - Gambas-it.org - Wikipedia

Europaeus sum !

<COLOR color="#FF8000">Amare memorentes atque deflentes ad mortem silenter labimur.</COLOR>
Online now: No Back to the top

Post

Posted
Rating:
#3
Regular
vuott is in the usergroup ‘Regular’
However, I would take this smoother path….

Code (gambas)

  1. Public Sub Form_Open()
  2.  
  3.   Dim path As String
  4.   Dim im As Image
  5.   Dim bb As Byte[]
  6.  
  7.   path = "/path/of/image/file"
  8.   im = Image.load(path)
  9.  
  10. ' ...I get the coveted byte array:
  11.   bb = Byte[].FromString(im.ToString(File.Ext(path), 100))
  12.  
  13. ' Test......
  14.   PictureBox1.Image = Image.FromString(bb.ToString(0, bb.Count))
  15.  

EDIT A POST : this my code is not good.

Europaeus sum !

<COLOR color="#FF8000">Amare memorentes atque deflentes ad mortem silenter labimur.</COLOR>
Online now: No Back to the top

Post

Posted
Rating:
#4
Enthusiast
AndyGable is in the usergroup ‘Enthusiast’

vuott said

However, I would take this smoother path….

Code (gambas)

  1. Public Sub Form_Open()
  2.  
  3.   Dim path As String
  4.   Dim im As Image
  5.   Dim bb As Byte[]
  6.  
  7.   path = "/path/of/image/file"
  8.   im = Image.load(path)
  9.  
  10. ' ...I get the coveted byte array:
  11.   bb = Byte[].FromString(im.ToString(File.Ext(path), 100))
  12.  
  13. ' Test......
  14.   PictureBox1.Image = Image.FromString(bb.ToString(0, bb.Count))
  15.  

Thank for the code vuott but I get a error message on   bb = Byte[].FromString(im.ToString(File.Ext(path), 100))

The error I get is "unkown symbol 'ToString' in class 'Image' (ImageControl:12)

What I am trying to do is convert the image into a Byte array so I can print it on a Epson Printer
Online now: No Back to the top

Post

Posted
Rating:
#5
Enthusiast
AndyGable is in the usergroup ‘Enthusiast’
This is the commands I am trying to follow (this is BASIC)

<IMG src="https://algpos.co.uk/gambas/ESC_PoSCommand.PNG"> </IMG>

I have my image file called StoreLogo.bmp so i would need to work out how to convert the image so I can print it on my printer

I KNOW I can upload the image to the Printers memory but the Tools for this all run only on Windows and as I am moving away from
Windows and My PoS Terminals only have powered USB ports now (and none of my exiting Windows PC have Powered USB I am not sure
how I can upload the image to the printer)

I have seen it where the Software would loads the image into the Printers RAM when started and print the image when needed (that is what I would like to do)

BUT as you all can see I am still struggling with getting it to work.
Online now: No Back to the top

Post

Posted
Rating:
#6
Regular
vuott is in the usergroup ‘Regular’

AndyGable said

The error I get is "unkown symbol 'ToString' in class 'Image' (ImageControl:12)
Probably you have to update your Gambas.
See:
Add 'Image.ToString()' and 'Picture.ToString()' methods. (3b21cbdb) · Commits · Gambas / gambas · GitLab

Europaeus sum !

<COLOR color="#FF8000">Amare memorentes atque deflentes ad mortem silenter labimur.</COLOR>
Online now: No Back to the top

Post

Posted
Rating:
#7
Regular
vuott is in the usergroup ‘Regular’
:? Perhaps you can find some other suggestions here:

Gambas One - Gambas ONE

Europaeus sum !

<COLOR color="#FF8000">Amare memorentes atque deflentes ad mortem silenter labimur.</COLOR>
Online now: No Back to the top

Post

Posted
Rating:
#8
Guru
BruceSteers is in the usergroup ‘Guru’
If you have an older gambas you can make your own function like this to convert an image to string…

Code (gambas)

  1.  
  2. Public Sub Whatever()
  3.  
  4.   Dim sImg As String = ImageToString(Image1)
  5.  
  6.  
  7. Public Sub ImageToString(Img As Image) As String
  8.  
  9.   Dim sTmp As String
  10.   Dim sData As String
  11.   sTmp = Env["XDG_RUNTIME_DIR"] &/  "temp.png")  ' get high speed memory dir, usually /run/user/1000
  12.   Img.Save(sTmp) ' Save image to temp png file (other formats can be used)
  13.   sData = File.Load(sTmp) ' load file as a string
  14.   Kill sTmp ' kill temp file
  15.   Return sData ' return string
  16.  
  17.  


But if you are loading from file name no need to use Image.class i guess you can just load the file directly?
Maybe like this…

Code (gambas)

  1. Public Sub Form_Open()
  2.  
  3.   Dim path As String
  4.   Dim bb As Byte[]
  5.  
  6.   path = "/path/of/image/file"
  7.  
  8. ' ...I get the coveted byte array:
  9.   bb = Byte[].FromString(File.Load(path))
  10.  
  11. ' Test......
  12.   PictureBox1.Image = Image.FromString(bb.ToString(0, bb.Count))
  13.    
  14.  
Online now: No Back to the top

Post

Posted
Rating:
#9
Regular
vuott is in the usergroup ‘Regular’

BruceSteers said

But if you are loading from file name no need to use Image.class i guess you can just load the file directly?
Maybe like this…

Code (gambas)

  1. Public Sub Form_Open()
  2.  
  3.   Dim path As String
  4.   Dim bb As Byte[]
  5.  
  6.   path = "/path/of/image/file"
  7.  
  8. ' ...I get the coveted byte array:
  9.   bb = Byte[].FromString(File.Load(path))
  10.  
  11. ' Test......
  12.   PictureBox1.Image = Image.FromString(bb.ToString(0, bb.Count))
  13.    
  14.  
Opsss…  :? I was also wrong with my code similar to yours: in this way he would upload the "file" and not only the data-byte strictly related to the image !   :oops:
In his initial code AndyGable states that he wants "the byte representation of the BMP image."

Europaeus sum !

<COLOR color="#FF8000">Amare memorentes atque deflentes ad mortem silenter labimur.</COLOR>
Online now: No Back to the top

Post

Posted
Rating:
#10
Regular
vuott is in the usergroup ‘Regular’
…having the "<COLOR color="#800000">BMP</COLOR>" format image file, if you need to send  to your printer the individual byte-data constituting the bitmap's pixel map structure, here is how to simply extract them from the file:

Code (gambas)

  1. Public Sub Form_Open()
  2.  
  3.   Dim w, h As Integer
  4.   Dim bit, saltus As Short
  5.  
  6.   s = File.Load("/path/of/file.bmp")
  7.  
  8.   w = Int@(s[18, SizeOf(gb.Integer)])
  9.   h = Int@(s[22, SizeOf(gb.Integer)])
  10.   bit = Short@(s[28, SizeOf(gb.Short)])
  11.   saltus = 14 + Int@(s[14, SizeOf(gb.Integer)])
  12.  
  13.   Print "Offset", "Data"
  14.   For i As Integer = saltus To (w * h * (bit / 8))
  15.     Print i, Hex(Byte@(s[i]), 2)   ' Let's look at the individual data-bytes
  16.     Wait 0.3
  17.   Next
  18.  

Europaeus sum !

<COLOR color="#FF8000">Amare memorentes atque deflentes ad mortem silenter labimur.</COLOR>
Online now: No Back to the top

Post

Posted
Rating:
#11
Enthusiast
AndyGable is in the usergroup ‘Enthusiast’
Can anyone convert Python code into Gambas Code?

Code

 def bitmap(self, data, length=0, density=0):
        if length == 0:
            length = len(data)
        if 0 < length < 1023:
            # length_lower = int(length & ord('\xff')).to_bytes()
            # length_higher = int((length & ord('\x03')<<8)>>8).to_bytes()
            
            # self.write(const.ESC_st + bytes(density) + length_lower + length_higher + bytes(data))
            self.write(const.ESC_st + density.to_bytes(1, "little") + length.to_bytes(2, "little") + bytes(data))
        else:
            raise Exception

So the command it's sending concats to
\x1B\x2A + (decimal value for density converted to bytes, assuming darkness or similar, but default is zero, little endian, length 1)  + (length of the data converted to bytes, little endian, length 2) + (data, converted to bytes)

This should then print my logo on the Epson Printer (as thanks to vuott code it converts it to Hex data)
Online now: No Back to the top
1 guest and 0 members have just viewed this.