Have you taken a look at the SaveImage library?
https://www.qb64.org/forum/index.php?topic=1651.0Useage is:
‘$INCLUDE:’SaveImage.BI’
result = SaveImageFull (“Filename.BMP”)
‘$INCLUDE:’SaveImage.BM’
Works with all screen modes, including screen 0 text screens. Saves in BMP, PNG, GIF, and PNG formats just by changing the extension to the desired format.
Other routines in the library allow converting from 32-bit images to 256 color images, saving segments of the screen and not just the whole image, and ZLIB compression/decompression routines. Most routines are written individually, so you can just extract the code you need for your project, if desired, and ignore all the rest of it. ;)