End graphics context after capturing screenshot

Also move screenshot code into a separate method.