GrabImage image,x,y[,frame]

Parameters

image - handle of an existing image to grab into

x - x coordinate in the current buffer to start grabbing from

y - y coordinate in the current buffer to start grabbing from

frame (optional) - frame of the image to store the grab in; 0 (default)

Description

Copies a rectangle of the current drawing buffer into an existing image.

The size of the grab comes from the image, not from the command: whatever the target image's width and height are, that is how much of the buffer is taken, starting at x,y. So to capture a 50x50 patch at 100,100 you first make a 50x50 image with CreateImage, then call GrabImage pic,100,100 and get the region from 100,100 to 150,150.

It is how you turn drawing into artwork. Compose something with Rect, Text and DrawImage, grab it, and from then on draw the finished thing in one call - handy for pre-rendered HUD panels, procedural tiles and starfields, or slicing a sheet you loaded into BackBuffer() into separate sprites. It works on any buffer, including an ImageBuffer, so you can grab out of one image into another.

The image must already exist - grabbing into a handle you have not created is an error - and the grab overwrites whatever that frame held. Coordinates are relative to the current Origin, and the target image's handle is taken into account, so an image with a centred handle grabs the region centred on x,y.

See also: CreateImage, ImageBuffer, SetBuffer, CopyRect, SaveImage.

Example

; GrabImage Example
; -----------------

Graphics 640,480,0,2
SetBuffer BackBuffer()

; A colourful photo to grab pieces from
logo=LoadImage("media/b3dlogo.jpg")

; GrabImage needs an existing image to grab INTO - its size (100x100)
; decides how much of the buffer is grabbed
stamp=CreateImage(100,100)
grabbed=0

While Not KeyDown(1)

    Cls

    DrawImage logo,120,140

    ; Keep the grab region fully on screen
    gx=MouseX()-50
    gy=MouseY()-50
    If gx<0 Then gx=0
    If gx>540 Then gx=540
    If gy<0 Then gy=0
    If gy>380 Then gy=380

    ; Space grabs the boxed region of the back buffer into the stamp image
    If KeyHit(57) Then
        GrabImage stamp,gx,gy
        grabbed=1
    End If

    ; Box the region that would be grabbed
    Color 255,255,0
    Rect gx,gy,100,100,False

    ; Show the grabbed image in the corner once we have one
    If grabbed=1 Then
        DrawImage stamp,530,10
        Color 255,255,255
        Rect 530,10,100,100,False
        Text 580,114,"grabbed",True
    End If

    Text 0,0,"Space: grab the boxed region   Esc: exit"
    Text 0,20,"GrabImage stamp,"+gx+","+gy

    Flip

Wend

End

Index