GrabImageScreen -function

Monkey Forums/Monkey Code/GrabImageScreen -function

V. Lehtinen(Posted 2014) [#1]
Hi all!

I'm not sure if this is here yet, but... I made a function that allows you to grab an image directly from the screen + a pixel masking function that I've found somewhere from the internets.


GrabScreen & GrabScreenMasked
Parameters:
- x,y is the position you want to grab from screen
- w,h is the size of your grabbed area and image size
- mR, mG, mG is for the color to be masked
EDIT: Removed pixel placing offsets since I noticed they wont work.
' Grab image from screen "buffer"
Function GrabScreen:Image(x:Int, y:Int, w:Int, h:Int)
    Local rtnImg:= CreateImage(w, h), pixels:= New Int[w * h]
    
    ReadPixels(pixels, x, y, w, h)
    rtnImg.WritePixels(pixels, 0, 0, w, h)
    
    Return rtnImg
End

' Grab image from screen "buffer" masked
Function GrabScreenMasked:Image(x:Int, y:Int, w:Int, h:Int, mR:Int = 0, mG:Int = 0, mB:Int = 0)
    Local rtnImg:= CreateImage(w, h), pixels:= New Int[w * h]
    
    ReadPixels(pixels, x, y, w, h)
    MaskPixels(pixels, mR, mG, mB)
    rtnImg.WritePixels(pixels, 0, 0, w, h)
    
    Return rtnImg
End


MaskPixels (I really can't remember where I found this... Sorry)
'// Converts Mask Pixel Color to Transparent Pixel
Function MaskPixels:Void(pixels:Int[], mask_r:Int = 0, mask_g:Int = 0, mask_b:Int = 0)
	For Local i : Int = 0 Until pixels.Length
    Local argb:Int = pixels[i]
    Local a : Int = ( argb Shr 24 ) & $ff
    Local r : Int = ( argb Shr 16 ) & $ff
    Local g : Int = ( argb Shr 8 )  & $ff
    Local b : Int = argb & $ff
    If r = mask_r And g = mask_g And b = mask_b
        a = 0
        argb = (a Shl 24) | (r Shl 16) | (g Shl 8) | b
        pixels[i] = argb
    Endif
	Next
End Functionn


Sincerely,
misthema


V. Lehtinen(Posted 2014) [#2]
Doublepost... This forum keeps f***ing with me!!