Generated png as hard from image and then load image to it
Posted: Fri Feb 06, 2026 8:28 pm
I would like to generate to image png in datasection and then to load it
Here the code example :
And in another code file for loading :
Work but it doesn't load image, say, 100 x 61 pixels ... How to resolve this ? Thanks in advance 
Here the code example :
Code: Select all
EnableExplicit
Define FileID, OutFileID
Define FileSize, i
Define Byte.b
Define Count = 0
; Open the PNG in binary mode
FileID = ReadFile(#PB_Any, "image.png")
If FileID = 0
MessageRequester("Error", "Cannot open image.png")
End
EndIf
FileSize = Lof(FileID)
; Create the output PB file
OutFileID = CreateFile(#PB_Any, "ImageData.pb")
If OutFileID = 0
MessageRequester("Error", "Cannot create ImageData.pb")
CloseFile(FileID)
End
EndIf
; DataSection header
WriteStringN(OutFileID, "DataSection")
WriteStringN(OutFileID, " ImagePNG:")
; Read PNG in binary
For i = 0 To FileSize - 1
Byte = ReadByte(FileID)
; New line every 64 values
If Count % 64 = 0
If Count > 0
WriteStringN(OutFileID, "")
EndIf
WriteString(OutFileID, " Data.b ")
Else
WriteString(OutFileID, ", ")
EndIf
WriteString(OutFileID, "$" + RSet(Hex(Byte & $FF), 2, "0"))
Count + 1
Next i
; End DataSection
WriteStringN(OutFileID, "")
WriteStringN(OutFileID, "EndDataSection")
CloseFile(FileID)
CloseFile(OutFileID)
MessageRequester("OK", "Raw PNG DataSection generated 👍")
End
Code: Select all
UsePNGImageDecoder()
;
; ==== > Put your DataSection here!
;
; -------------------------------
; Usage in the program
; -------------------------------
; Load the image from the DataSection
If CatchImage(0, ?imagePNG)
; Create a window to display the image
If OpenWindow(0, 100, 100, 200, 200, "Display PNG from DataSection")
ImageGadget(0, 10, 10, 180, 180, 0)
; Attach the captured image to the image gadget
SetGadgetState(0, #True) ; refresh display
StartDrawing(ImageOutput(0))
DrawImage(0, 0, 0, 100, 61)
StopDrawing()
; Wait loop
Repeat
Until WaitWindowEvent() = #PB_Event_CloseWindow
EndIf
Else
MessageRequester("Error", "Cannot load PNG from DataSection")
EndIf