Please have a look
The purpose of this code is not to take screenshot, but I think you can adapt it to your needs.