It works but should I be using Base64EncoderBuffer, [size = FileSize(filename$) * 1.35], or some other allocation to avoid overflows? I'm not yet adept at using memory functions

Also, am I using StringByteLength in the right place (line 55) or should it be used in the encoding procedure?
Code: Select all
UseTIFFImageDecoder()
UseJPEGImageDecoder()
UsePNGImageDecoder()
UseGIFImageDecoder()
UseJPEG2000ImageDecoder()
UseTGAImageDecoder()
Procedure.s base64encodefile(filename$) ; Encode a file to a base64 string.
file = ReadFile(#PB_Any, filename$)
Select file
Case 0
Default
size = FileSize(filename$); * 1.35 ; FileSize and Lof produce the same result
*filebuff = AllocateMemory(size) ; Allocate a memory block the size of the file
ReadData(file, *filebuff, size) ; Read the file into the memory block
buffer$ = Base64Encoder(*filebuff, size) ; Base64 encode the buffer into the variable
FreeMemory(*filebuff)
CloseFile(file)
EndSelect
Debug buffer$
ProcedureReturn buffer$
EndProcedure
Procedure base64catchimage(buffer$ , buffersize) ; Create a new image from a base64 string.
*output = AllocateMemory(buffersize) ; Allocate a memory block the size of the string (buffer$)
Base64Decoder(buffer$, *output , buffersize) ; Decode the string (buffer$) into memory (*output) with the size provided
image = CatchImage(#PB_Any, *output) ; Output an ImageID from Base64 decoded memory
FreeMemory(*output)
ProcedureReturn image
EndProcedure
filename$ = OpenFileRequester("" , "" , "" , 0)
Select Len(filename$) ; End if filename is blank
Case 0
Default
buffer$ = base64encodefile(filename$)
image = base64catchimage(buffer$ , StringByteLength(buffer$))
OpenWindow(0 , 0 , 0 , 0 , 0 , "" , #PB_Window_ScreenCentered | #PB_Window_SystemMenu)
ImageGadget(0 , 0 , 0 , 0 , 0 , ImageID(image))
ResizeWindow(0 , #PB_Ignore , #PB_Ignore , ImageWidth(image) , ImageHeight(image))
Repeat
event = WaitWindowEvent()
Select event
Case #PB_Event_CloseWindow
shutdown = #True
EndSelect
Until shutdown = #True
EndSelect