SaveImageContributor(s):
@SMcNeillSource: qb64.org Forum
URL:
https://www.qb64.org/forum/index.php?topic=2701.0Tags: [bmp] [export] [gif] [graphics] [jpg] [png] [zlib]
Description:
How to save a QB64 screen image in either BMP or PNG format; how one can easily turn a text screen into a 256 color graphic screen with one simple command; and how to compress and inflate strings or programs quickly and easily.
There's a lot of stuff packaged inside the library here, but the main ones which a user might decide to call is the following:
FUNCTION SaveImage (file$, image&, x1%, y1%, x2%, y2%)
SUB SaveFullImage (filename$)
SUB SaveFullBMP (filename$)
SUB SaveFullJPG (filename$)
SUB SaveBMP (filename$, image&, x1%, y1%, x2%, y2%)
FUNCTION TextScreenToImage256& (image&)
FUNCTION TextScreenToImage32& (image&)
FUNCTION Deflate$ (text$)
FUNCTION Inflate$ (text$)
FUNCTION PNGExport (file$, imagehandle%, x1%, y1%, x2%, y2%)
SUB SaveJPG (file$, image&, startx, starty, finishx, finishy)
As you can see, most of these routines can easily be handled by simply using FUNCTION SaveImage, which then decides if it needs to convert from a text screen to a graphic screen, and which save routine to make use of.Source Code:
'$INCLUDE:'SaveImage.BI'
CONST SaveTextAs256Color
= 0 'Flag to Save as 256 color file or 32-bit color file, when converting SCREEN 0 to an image ' Set to TRUE (any non-zero value) to save text screens in 256 color mode.
' Set to FALSE (zero) to save text screens in 32-bit color mode.
'CONST ConvertToStandard256Palette = 0
' Set the value to 0 (FALSE) to preseve the color information perfectly, using its default palette.
' If the CONST is set (TRUE), then we convert our colors to as close of a match as possible, while
' preserving the standard QB64 256-color palette.
' Commented here, simply to help folks know that it exists for use when converting a 32 bit image
' down to 256 colors, such as what the GIF routine has to do for us (GIFs are limited to 256 color images)
InitialImage$ = "Volcano Logo.jpg"
exportimage(1) = "testimage.png": exportimage(2) = "testimage.bmp"
exportimage(3) = "testimage.jpg": exportimage(4) = "testimage.gif"
'If you want to test the demo with a screen 0 image, then...
l&
= _LOADIMAGE(InitialImage$
) 'Remark out this line'_PUTIMAGE , l& 'And remark the _PUTIMAGE line down below as well
'And unremark the following
'SCREEN 0
'FOR i = 0 TO 15
' COLOR i
' PRINT "COLOR i"
'NEXT
'Then you can watch as we prove that we can save images while in Screen 0 TEXT mode.
_PUTIMAGE , l&
'Remark out this line, if you want to see the SCREEN 0 demo Result
= SaveImage
(exportimage
(i
), 0, 0, 0, _WIDTH - 1, _HEIGHT - 1) IF Result
= 1 THEN 'file already found on drive KILL exportimage
(i
) 'delete the old file Result
= SaveImage
(exportimage
(i
), 0, 0, 0, _WIDTH - 1, _HEIGHT - 1) 'save the new one again IF Result
< 0 THEN PRINT "Successful " + exportimage
(i
) + " export" ELSE PRINT ext$
+ " Export failed with Error Code:"; Result:
' END
PRINT "Image Handle: "; zz&
, exportimage
(i
) PRINT "Successful Import using _LOADIMAGE" PRINT "ERROR - Not Loading the new image (" + exportimage
(i
) + ") with _LOADIMAGE."
'$INCLUDE:'SaveImage.BM'
Check out the latest update at the author's Project File in the URL above.