Convert image to byte array - what am I doing wrong.
Posted
#1
(In Topic #1176)
Enthusiast

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)
- ' Load the BMP image into the Image view
- ' Convert the BMP image to a byte array
- ' Now 'byteArray' contains the byte representation of the BMP image
- ' Create a memory stream to store the image data
- ' Save the image to the memory stream
- ' Seek to the beginning of the memory stream
- ' Read the byte array from the memory stream
- ' Close the memory stream
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?
Posted
Regular

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>
<COLOR color="#FF8000">Amare memorentes atque deflentes ad mortem silenter labimur.</COLOR>
Posted
Regular

Code (gambas)
EDIT A POST : this my code is not good.
Europaeus sum !
<COLOR color="#FF8000">Amare memorentes atque deflentes ad mortem silenter labimur.</COLOR>
<COLOR color="#FF8000">Amare memorentes atque deflentes ad mortem silenter labimur.</COLOR>
Posted
Enthusiast

vuott said
However, I would take this smoother path….Code (gambas)
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
Posted
Enthusiast

<IMG src="https://algpos.co.uk/gambas/ESC_PoSCommand.PNG">
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.
Posted
Regular

Probably you have to update your Gambas.AndyGable said
The error I get is "unkown symbol 'ToString' in class 'Image' (ImageControl:12)
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>
<COLOR color="#FF8000">Amare memorentes atque deflentes ad mortem silenter labimur.</COLOR>
Posted
Regular

Europaeus sum !
<COLOR color="#FF8000">Amare memorentes atque deflentes ad mortem silenter labimur.</COLOR>
<COLOR color="#FF8000">Amare memorentes atque deflentes ad mortem silenter labimur.</COLOR>
Posted
Guru

Code (gambas)
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…
Posted
Regular

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 !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…
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>
<COLOR color="#FF8000">Amare memorentes atque deflentes ad mortem silenter labimur.</COLOR>
Posted
Regular

Code (gambas)
- Wait 0.3
Europaeus sum !
<COLOR color="#FF8000">Amare memorentes atque deflentes ad mortem silenter labimur.</COLOR>
<COLOR color="#FF8000">Amare memorentes atque deflentes ad mortem silenter labimur.</COLOR>
Posted
Enthusiast

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)
1 guest and 0 members have just viewed this.


