GETBMP

From Liberty BASIC Family
Jump to navigation Jump to search
Supported in Just BASIC Supported in Liberty BASIC Not supported in Liberty BASIC 5 Not supported in Run BASIC

Description

Copies the graphics from a graphics window or graphicbox control and makes them into a bitmap. This bitmap resides in memory.

Syntax

  1. #handle.ext "getbmp name x y width height"
    • name an alias you give the bmp for identification.
    • x y the coordinates of the upper left corner where to begin the capture.
    • width height the width and height in pixels of the area to be copied.

Hints

Once you use getbmp, you can use drawbmp to display it to a window; or, you can use bmpsave to save the image to disk.
To retain the displayed graphics in a window, be sure to flush the graphics.
When the copied image is no longer needed, use unloadbmp to remove the image from memory.

These commands are generally used when working with bitmaps:

Example

nomainwin

WindowWidth=400
WindowHeight=300

    open "Getbmp Test" for graphics_nf_nsb as #g
    #g "trapclose [quit]"

    'fill background with color
    #g "fill cyan"
    'print some text
    #g "font arial 36"
    #g "down;color lightgray;backcolor cyan; place 20 150"
    #g "\DO NOT COPY"

    'capture the displayed image with alias 'noCopy'
    #g "getbmp noCopy 0 0 390 270"
    'save image to disk as bmp file
    bmpsave "noCopy", "nocopy.bmp"
    wait

[quit]
    'remove image from memory
    unloadbmp "noCopy"
    close #g
    end

Useful Procedures

With GETBMP, this function allows to read color of a point in graphicbox / graphic window:

Warning. Because it saves pixel to file on disk, it's pretty slow - my tests shows about 500 pixels per second on 3GHz box. Just be aware of it.

'GetPixelValue$ returns a string with the RGB values of the pixel
'in coordinates x and y in window/graphicbox names handle$ (e.g, "#main.graph")
function GetPixelValue$(x, y, handle$)

'Grab a 1*1 bitmap
    #handle$, "getbmp gpv "; x; " "; y; " "; 1; " "; 1

'Save in a bmp file
    bmpsave "gpv", "getpvaluetemp.bmp"

'Open the file for string input and get it's full contents
    open "getpvaluetemp.bmp" for input as #gpv
    s$ = input$(#gpv, lof(#gpv))
    close #gpv

'Check if user's display is 32-bit, and read the red-green-blue values
'If display 16 bit, then colors are masked. So some last (3 for red, 2 for green, 3 for blue) bits always 0
'That means that you did not get 255 255 255 for white - (248 252 248) instead. You have to experiment
'otherwise function returns nothing (support for other display types could be added (?))
    bpp =  asc(mid$(s$, 29, 1))
    select case bpp
    case 32
        red = asc(mid$(s$, 69, 1))
        green = asc(mid$(s$, 68, 1))
        blue = asc(mid$(s$, 67, 1))
    case 16
        bytes = asc(mid$( s$, 67, 1)) + 256*asc(mid$( s$, 68, 1))
        red =  (bytes AND 63488) /256       '0xF800
        green =  (bytes AND 2016) / 32 * 4  '0x7E0
        blue =  (bytes AND 31) * 8          '0x1F
    end select

'concatenate the return value, delete temporary file and free memory
    GetPixelValue$ = str$(red)+" "+str$(green)+" "+str$(blue)
    kill "getpvaluetemp.bmp"
    unloadbmp "gpv"
end function