<- Go back
Save Image Library

Author: SMcNeill
Source: QB64 Forum
URL: http://www.qb64.net/forum/index.php?topic=13479.0
Release Date: 2016
Tags: image, save, library
Download from qb64.org: SaveImage.7z

Some of you may have already noticed the PNG Export library I posted earlier...  It's now gone completely.

Instead, I now offer the Save Image Library, for your free use and enjoyment!!

"Why the new library?", you ask.

Because it's now does a whole lot more than just PNG files!

Format for the command is the same (actually simpler, as we now auto-detect alpha channels and use RGBA or RGB automagically as needed), and looks like the following:

Result = SaveImage(exportimage1$, 0, 0, 0, _WIDTH, _HEIGHT)

The actual function is the following:   FUNCTION SaveImage (file$, image&, x1%, y1%, x2%, y2%)

So, how is this different than before?

First, it saves both PNG and BMP files for us.  Just specify the extension for which you want in the filename.

exportimage1$ = "testimage.png"
exportimage2$ = "testimage.bmp"

With the above, the first filename will export a PNG file, while the second will export a BMP file.   Change the extension, change the output format.  It's that simple.

Second, this now works in ALL the QB64 screen modes for both PNG and BMP export images.  That's right:  It can even export a screen capture of a SCREEN 0, TEXT screen for you!!

As a side note, the PNG side of things does a better job error-checking itself and all, since it also does file compression and all that stuff, so don't expect to get any error messages from saving a BMP file. 

PNG files can return the following error messages for you:

    'PNGExport results:
    '-1   All is good.  We think we exported a proper PNG file.
    ' 0   Compression failed.  File is probably corrupt.
    ' 1   File Already Exists.  As these are Binary files, we probably don't want to just overwrite the same file.
    ' 2   Incorrect Alpha settings.  Check for Alpha mode, and Color mode, and look for conflicts.
    ' 3   Bad GrabMode
    ' 4   Bad x1 coordinate
    ' 5   Bad y1 coordinate
    ' 6   Bad x2 coordinate
    ' 7   Bad y2 coordinate
    ' 8   x2 < x1 -- correct this to proceed
    ' 9   y2 < y1 -- correct this to proceed
    '10   Bad color mode.  Either use 256 or 32 color mode.
    '11   Attempted to export a text screen which will not work.

BMP files will always return a -1.  If they fail, it'll be a standard QB64 error message of some sort which will pop up.

Grab the attachment below, extract to your QB64 folder, and give it a go and see how it works for you.  From my testing, this works for all screen modes, in both SDL and GL versions, and hasn't died on me yet. 

All code is free to use, alter, delete, edit, and annoy Clippy with.  No credit expected, nor needed.  If you can use it, use it.  If you can sell it and make $$$ with it, do that too!  Just be warned:  If it somehow melts your computer or starts a zombie takeover, I take no responsibility for that either.  Code is "As is -- As is, you use it however you want, but you're responsible for whatever happens when you do."   :D

Usage is:

'$Include:'SaveImage.BI'    <--- Top of your code
Result = SaveImage(exportimage1$, 0, 0, 0, _WIDTH, _HEIGHT)  <--- Inside the code somewhere
'$INCLUDE:'SaveImage.BM'    <---  Bottom of your code