You are reading outside the screen i think. You should change that to:
Having said that, this isn't the fastest way. skidracer explains a faster solution above.
lockbuffer backbuffer() For x = 0 to GraphicsWidth()-1 For Y = 0 to GraphicsHeight()-1 thisPixel = ReadPixelFast(x,y,backbuffer()) And $FFFFFF next next unlockbuffer backbuffer()
Having said that, this isn't the fastest way. skidracer explains a faster solution above.