Page 1 of 2
Image Size and SAVEIMAGE Problem [Resolved]
Posted: Sun Dec 31, 2023 8:31 am
by tikidays
Sorry if this is a noob question but I have hit a knowledge brick wall and spent the day searching the forums.
How do I get the the size in bytes of an image in memory? Im trying to encode it as a string. In desperation I tried using SAVEIMAGE to the temp directory and using FILESIZE to get the image size, but this resulted in a new problem where SAVEIMAGE is not creating the PNG, not sure why this isn't working. But if I can avoid doing this even better.
Code: Select all
Procedure.s encodePNG(image.i)
fileName.s = GetTemporaryDirectory()+"temp.png"
If SaveImage(image,fileName,#PB_ImagePlugin_PNG)
size.i = FileSize(fileName)
encoded.s = Base64Encoder(image, size)
ProcedureReturn encoded
Else
Debug "Save error"
EndIf
EndProcedure
PureBasic 6.04 LTS - C Backend (MacOS X - arm64)
Re: Image Size and SAVEIMAGE Problem
Posted: Sun Dec 31, 2023 8:42 am
by boddhi
When you want to save your image in PNG format, you need to use UsePNGImageEncoder().
Re: Image Size and SAVEIMAGE Problem
Posted: Sun Dec 31, 2023 9:10 am
by wombats
Check out the EncodeImage command.
Re: Image Size and SAVEIMAGE Problem
Posted: Sun Dec 31, 2023 9:18 am
by Little John
@tikidays:
Please don't ask the same coding question in 3 different forum threads. TIA.
Re: Image Size and SAVEIMAGE Problem
Posted: Sun Dec 31, 2023 5:54 pm
by tikidays
Little John wrote: Sun Dec 31, 2023 9:18 am
@tikidays:
Please don't ask the same coding question in 3 different forum threads. TIA.
Apologies, but they were a progression of questions as I looked into it more.
Re: Image Size and SAVEIMAGE Problem
Posted: Sun Dec 31, 2023 5:58 pm
by tikidays
boddhi wrote: Sun Dec 31, 2023 8:42 am
When you want to save your image in PNG format, you need to use UsePNGImageEncoder().
Thanks for the heads-up, I had no idea I had to invoke this again inside a new procedure! I assumed once at the start of the code was it.
Re: Image Size and SAVEIMAGE Problem
Posted: Sun Dec 31, 2023 6:00 pm
by tikidays
wombats wrote: Sun Dec 31, 2023 9:10 am
Check out the EncodeImage command.
Thanks so much, I had seen this but was unsure how it could help, but re-reading it this will solve my problem, thanks a lot!
Re: Image Size and SAVEIMAGE Problem
Posted: Sun Dec 31, 2023 6:13 pm
by Little John
tikidays, wrote:I assumed once at the start of the code was it.
This assumption was/is right:
UsePNGImageEncoder() needs only to be called once in a program (normally somewhere at the beginning).
Re: Image Size and SAVEIMAGE Problem
Posted: Sun Dec 31, 2023 6:28 pm
by tikidays
Little John wrote: Sun Dec 31, 2023 6:13 pm
tikidays, wrote:I assumed once at the start of the code was it.
This assumption was/is right:
UsePNGImageEncoder() needs only to be called once in a program (normally somewhere at the beginning).
Thanks, rechecking I discovered I had moved this command into a LoadImages procedure, moved it back out.
Re: Image Size and SAVEIMAGE Problem
Posted: Sun Dec 31, 2023 6:49 pm
by boddhi
@tikidays
F1 is a magic key !
Sometimes when you invoke it, it makes problems disappear !

Re: Image Size and SAVEIMAGE Problem
Posted: Sun Dec 31, 2023 7:02 pm
by tikidays
tikidays wrote: Sun Dec 31, 2023 6:28 pm
Little John wrote: Sun Dec 31, 2023 6:13 pm
tikidays, wrote:I assumed once at the start of the code was it.
This assumption was/is right:
UsePNGImageEncoder() needs only to be called once in a program (normally somewhere at the beginning).
Thanks, rechecking I discovered I had moved this command into a LoadImages procedure, moved it back out.
Correction I had to re-add it for the encoder to work, if I place it in the parent Procedure its happy. I wouldn't have expected this behaviour.
Re: Image Size and SAVEIMAGE Problem
Posted: Sun Dec 31, 2023 7:40 pm
by Little John
in orfder to make things not more complicated than necessary, put UsePNGImageEncoder() in the main scope of the program, not in a procedure.
Re: Image Size and SAVEIMAGE Problem
Posted: Sun Dec 31, 2023 10:02 pm
by boddhi
@tikidays
As said Little John, to be safe, place this instruction outside and before all image-related procedures and commands.

Re: Image Size and SAVEIMAGE Problem
Posted: Mon Jan 01, 2024 6:39 am
by tikidays
Do I place both
Code: Select all
UsePNGImageEncoder()
UsePNGImageDecoder()
At the start?
The encoding seemed straight forward but my decoder is still not working...the window is to see what image it decodes, but it's showing some random, not what was encoded. :/ I think I have a fundamental mis-understanding of how to address images?
Code: Select all
Procedure decodePNG(PNG.s,size.i)
Debug "Decoding: " + PNG
size * 0.65 : If size < 64 : size = 64 : EndIf
*buffer = AllocateMemory(size)
If *buffer
If imageSize = Base64Decoder(PNG,*buffer,size)
image = CatchImage(#PB_Any, *buffer)
OpenWindow(#PB_Any,0,0,1024,1024,"Decoded image "+Str(imageSize/1024)+"kb")
ImageGadget(#PB_Any,0,0,1024,1024,ImageID(image))
FreeMemory(*buffer)
ProcedureReturn image
Else
Debug "imageSize : Decode failed."
EndIf
Else
Debug "*buffer : Decode failed."
EndIf
FreeMemory(*buffer)
EndProcedure
Re: Image Size and SAVEIMAGE Problem
Posted: Mon Jan 01, 2024 7:08 am
by tikidays
boddhi wrote: Sun Dec 31, 2023 6:49 pm
@tikidays
F1 is a magic key !
Sometimes when you invoke it, it makes problems disappear !
you mean fn-F1 for my Mac
