BSAVE
From QB64 Wiki
BSAVE saves the contents of an image array to a BINARY file.
- BSAVE}} savefile$, VARPTR(Array(index)), Filesize&
Description:
- 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 #.
- Filesize must be twice the size of the elements used in an INTEGER array.
- BSAVE files can later be opened with BLOAD.
Example 1: 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 DEF SEG
- 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.
See also:
- BLOAD, OPEN, BINARY
- GET, PUT (file statements)
- GET (graphics statement), PUT (graphics statement)
- VARSEG, VARPTR
- DEF SEG, TYPE
- Text Using Graphics