Hi Karl, This seems strange to me too. I tested your code and found that the screenshot always showed the pre-agenda state of the frame, showing the lisp window at the top and the helm-M-x window at the bottom, the way it looked when I hit RET to run the command.
However, I tried stepping through the function with edebug, and then it worked properly: the screenshot was taken after the agenda displayed, and it showed the agenda in the screenshot. I don't know much about Emacs internals, but I'm guessing that something's preventing the frame from being repainted until the function finishes. Maybe you could use a separate function to take the screenshot and call it with a timer?