I think it would be much better to move this image generation code to a new private function. Now this method looks too long - it does too many things. Ideally a method should do only one thing. This one is making an HTTP request to load XML, parses XML, builds image, and returns image. Too many responsibilities for one method.