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

Post your Gambas programming questions here.
AndyGable
Posts: 363
Joined: Wednesday 2nd December 2020 12:11am
Location: Northampton, England
Contact:

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

Post by AndyGable »

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

Public Sub ConvertImage()
    Dim bmpFilePath As String = "till_logo.bmp"
    Dim byteArray As Byte[]
   
    ' Load the BMP image into the Image view
    FMain.ImageView1.Image = Image.Load(bmpFilePath)
   
    ' Convert the BMP image to a byte array
    byteArray = ImageToByteArray(FMain.ImageView1.Image)
   
   
   
    ' Now 'byteArray' contains the byte representation of the BMP image
End

Private Function ImageToByteArray(image As Image) As Byte[]
    Dim stream As File
   
    ' Create a memory stream to store the image data
    stream = Open Memory image For Read Write

    ' Save the image to the memory stream
    image.Save(stream)
   
    ' Seek to the beginning of the memory stream
    stream.Seek(0)
   
    ' Read the byte array from the memory stream
    Return ImageToByteArray = stream.Read(stream.Length)
   
   ' Close the memory stream
    stream.Close
End



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?
vuott
Posts: 263
Joined: Wednesday 5th April 2017 6:07pm
Location: European Union

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

Post by vuott »

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.

http://gambaswiki.org/wiki/lang/memory?nh
https://gambaswiki.org/wiki/lang/byte@
https://www.gambas-it.org/wiki/index.ph ... ory_Stream
https://www.gambas-it.org/wiki/index.ph ... ory_Stream
Europaeus sum !

Amare memorentes atque deflentes ad mortem silenter labimur.
vuott
Posts: 263
Joined: Wednesday 5th April 2017 6:07pm
Location: European Union

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

Post by vuott »

However, I would take this smoother path....
Public Sub Form_Open()

  Dim path As String
  Dim im As Image
  Dim bb As Byte[]

  path = "/path/of/image/file"
  im = Image.load(path)

' ...I get the coveted byte array:
  bb = Byte[].FromString(im.ToString(File.Ext(path), 100))

' Test......
  PictureBox1.Image = Image.FromString(bb.ToString(0, bb.Count))
  
End


EDIT A POST : this my code is not good.
Last edited by vuott on Friday 19th January 2024 1:22am, edited 1 time in total.
Europaeus sum !

Amare memorentes atque deflentes ad mortem silenter labimur.
AndyGable
Posts: 363
Joined: Wednesday 2nd December 2020 12:11am
Location: Northampton, England
Contact:

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

Post by AndyGable »

vuott wrote: Thursday 18th January 2024 3:49pm However, I would take this smoother path....
Public Sub Form_Open()

  Dim path As String
  Dim im As Image
  Dim bb As Byte[]

  path = "/path/of/image/file"
  im = Image.load(path)

' ...I get the coveted byte array:
  bb = Byte[].FromString(im.ToString(File.Ext(path), 100))

' Test......
  PictureBox1.Image = Image.FromString(bb.ToString(0, bb.Count))
  
End
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
AndyGable
Posts: 363
Joined: Wednesday 2nd December 2020 12:11am
Location: Northampton, England
Contact:

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

Post by AndyGable »

This is the commands I am trying to follow (this is BASIC)

Image

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.
vuott
Posts: 263
Joined: Wednesday 5th April 2017 6:07pm
Location: European Union

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

Post by vuott »

AndyGable wrote: Thursday 18th January 2024 7:52pm The error I get is "unkown symbol 'ToString' in class 'Image' (ImageControl:12)
Probably you have to update your Gambas.
See:
https://gitlab.com/gambas/gambas/-/comm ... b1e4c46e08
Europaeus sum !

Amare memorentes atque deflentes ad mortem silenter labimur.
vuott
Posts: 263
Joined: Wednesday 5th April 2017 6:07pm
Location: European Union

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

Post by vuott »

:? Perhaps you can find some other suggestions here:

https://forum.gambas.one/viewtopic.php?t=574
Europaeus sum !

Amare memorentes atque deflentes ad mortem silenter labimur.
User avatar
BruceSteers
Posts: 1578
Joined: Thursday 23rd July 2020 5:20pm
Location: Isle of Wight
Contact:

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

Post by BruceSteers »

If you have an older gambas you can make your own function like this to convert an image to string...


Public Sub Whatever()

  Dim sImg As String = ImageToString(Image1)

End

Public Sub ImageToString(Img As Image) As String

  Dim sTmp As String
  Dim sData As String
  sTmp = Env["XDG_RUNTIME_DIR"] &/  "temp.png")  ' get high speed memory dir, usually /run/user/1000
  Img.Save(sTmp) ' Save image to temp png file (other formats can be used)
  sData = File.Load(sTmp) ' load file as a string
  Kill sTmp ' kill temp file
  Return sData ' return string

End



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...

Public Sub Form_Open()
 
  Dim path As String
  Dim bb As Byte[]
 
  path = "/path/of/image/file"
 
' ...I get the coveted byte array:
  bb = Byte[].FromString(File.Load(path))
 
' Test......
  PictureBox1.Image = Image.FromString(bb.ToString(0, bb.Count))
   
End
If at first you don't succeed , try doing something differently.
BruceS
vuott
Posts: 263
Joined: Wednesday 5th April 2017 6:07pm
Location: European Union

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

Post by vuott »

BruceSteers wrote: Friday 19th January 2024 12:36am 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...

Public Sub Form_Open()
 
  Dim path As String
  Dim bb As Byte[]
 
  path = "/path/of/image/file"
 
' ...I get the coveted byte array:
  bb = Byte[].FromString(File.Load(path))
 
' Test......
  PictureBox1.Image = Image.FromString(bb.ToString(0, bb.Count))
   
End
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 !

Amare memorentes atque deflentes ad mortem silenter labimur.
vuott
Posts: 263
Joined: Wednesday 5th April 2017 6:07pm
Location: European Union

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

Post by vuott »

...having the "BMP" 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:
Public Sub Form_Open()

  Dim s As String
  Dim w, h As Integer
  Dim bit, saltus As Short
  
  s = File.Load("/path/of/file.bmp")
  
  w = Int@(s[18, SizeOf(gb.Integer)])
  h = Int@(s[22, SizeOf(gb.Integer)])
  bit = Short@(s[28, SizeOf(gb.Short)])
  saltus = 14 + Int@(s[14, SizeOf(gb.Integer)])

  Print "Offset", "Data"
  For i As Integer = saltus To (w * h * (bit / 8))
    Print i, Hex(Byte@(s[i]), 2)   ' Let's look at the individual data-bytes
    Wait 0.3
  Next

End
Europaeus sum !

Amare memorentes atque deflentes ad mortem silenter labimur.
Post Reply