QB64 is a modern extended BASIC programming language that retains QBasic/QuickBASIC 4.5 compatibility and compiles native binaries for Windows, Linux, and macOS.

BSAVE saves the contents of an image array to a BINARY file.

Legacy Support


BSAVE saveFile$, VARPTR(array(index)), fileSize&




Saving array data to a file quickly.

LB% = LBOUND(Array)
bytes% = LEN(Array(LB%))
filesize& = ((UBOUND(Array) - LB%) + 1) * bytes% 
DEF SEG = VARSEG(Array(0))
BSAVE filename$, VARPTR(Array(LB%)), filesize&  ' changeable index

Explanation: Procedure determines the filesize from the array size automatically. LBOUND is used with UBOUND to determine array size and byte size. Works with any type of array except variable-length strings. Used for saving program data fast.

BSAVEing a bitmap and calculating the file size

 DEF SEG = VARSEG(Image(0))
 PSET(BMPHead.PWidth - 1, BMPHead.PDepth - 1)  'color lower right corner if black
 GET (0, 0)-(BMPHead.PWidth - 1, BMPHead.PDepth - 1), Image(NColors * 3) ' for 16 or 256 colors
 FOR a& = 26000 TO 0 STEP -1
   IF Image(a&) THEN ArraySize& = a&: EXIT FOR
 BSAVE SaveName$, VARPTR(Image(0)), (2 * ArraySize&) + 200 'file size

Explanation: The FOR…NEXT loop reads backwards through the image array until it finds a value not 0. The LONG ArraySize& value is doubled and 200 is added. BMPhead.PWidth and BMPhead.PDepth are found by reading the bitmap’s information header using a TYPE definition. See Bitmaps.

Using PUT and GET to write and read array data from a file without using BSAVE or BLOAD:

KILL "example2.BIN" 'removes old image file!

REDIM Graphic%(1001) 'REDIM makes array resize-able later

LINE (0, 0)-(10, 10), 12, B 'create image
GET(0, 0)-STEP(10, 10), Graphic%() 'get image to array

FOR i% = 1000 TO 0 STEP -1 'reverse read array for size needed
    IF Graphic%(i%) <> 0 THEN EXIT FOR 'find image color not black
size% = i% + 4 'size plus 2 integers(4  bytes) for dimensions 
REDIM _PRESERVE Graphic%(size%) 'resize existing array in QB64 only!

OPEN "example2.BIN" FOR BINARY AS #1 ' PUT to a file
PUT #1, , Graphic%()

OPEN "example2.BIN" FOR BINARY AS #2 'GET array and PUT to screen
DIM CopyBin%(LOF(2) \ 2) 'create new array sized by half of file size
GET #2, , CopyBin%()
PUT(100, 100), CopyBin%(), PSET
fsize% = LOF(2)

K$ = INPUT$(1) 'Press any key 
FOR i = 0 TO 20 'read all 3 arrays
    PRINT Graphic%(i); CopyBin%(i)
PRINT "Array:"; size%, "File:"; fsize%

Explanation: A 10 by 10 pixel box is saved to an array using the GET (graphics statement) and written to a BINARY file using PUT #1. Then GET #1 places the file contents into another INTEGER array and places it on the screen with the PUT (graphics statement).

The array contents: 88 is the width in the GET array for SCREEN 13 which needs divided by 8 in that mode only. The area is actually 11 X 11. The array size needed can be found by looping backwards through the array until a color value is found. IF array(i) <> 0 THEN EXIT FOR (66 integers) or by dividing the created BINARY file size in half (134 bytes) when known to be array sized already.

See Also