From QB64 Wiki
BSAVE saves the contents of an image array to a BINARY file.
- BSAVE savefile$, VARPTR(Array(index)), Filesize&
- savefile$ is the STRING file name of the file designated to be created.
- Array(index) is the image array that already holds the GET image data.
- Filesize must be a bit over twice the size of the elements used in an INTEGER array.
- To place image data into the array, use GET to store a box area image of the screen.
- SCREEN 12 can only GET 1/3 of the screen image at one time using a 26K array.
- Image arrays are DIMensioned as INTEGER. Use DEFINT when working with large graphic arrays.
- Any arrays can be saved, but Image arrays are most common.
- DEF SEG = VARSEG must be used to designate the array segment position in memory.
- VARPTR returns the Array index offset of the memory segment. Array sizes are limited to 32767 Integer elements due to the use of VARPTR in QB and QB64!.
- QB64 can load larger arrays directly to and from binary files using PUT # and GET # without BSAVE.
- BSAVE files can later be opened with BLOAD.
Example 1: Saving array data to a file quickly.
- 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. Great for saving program data fast!
Example 2: 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 NEXT BSAVE SaveName$, VARPTR(Image(0)), (2 * ArraySize&) + 200 'file size DEF SEG
- Explanation: The FOR 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.
KILL "example2.BIN" 'removes old image file! SCREEN 13 OPTION BASE 0 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 NEXT 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%() CLOSE 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) CLOSE K$ = INPUT$(1) 'Press any key FOR i = 0 TO 20 'read all 3 arrays PRINT Graphic%(i); CopyBin%(i) NEXT 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.
- GET (graphics statement), PUT (graphics statement)
- BLOAD, OPEN, BINARY
- GET, PUT (file statements)
- VARSEG, VARPTR
- DEF SEG, TYPE
- Text Using Graphics