Thank you Alex. I got there in the end with a bit of work. Below is the method I have. I found that a good thing to do was to set imageWidth and imageHeight, by fixing in my case the imageWidth using the following:
ReferencedEnvelope re = mapContent.getMaxBounds(); double height = re.getHeight(); double width = re.getWidth(); System.out.println("height " + height + ", width " + width); double heightToWidth = height / width; int imageWidth = 500; int imageHeight = (int) (imageWidth * heightToWidth); /** * Render and output the mapContent and save to a file. * @param mapContent The map to be written. * @param imageWidth The width of the image to be produced. * @param imageHeight The height of the image to be produced. * @param outputImageFile The file to be written to. * @param outputType The image file type e.g. "png" */ public void writeImageFile( MapContent mapContent, int imageWidth, int imageHeight, File outputImageFile, String outputType) { // Initialise a renderer StreamingRenderer renderer = new StreamingRenderer(); renderer.setMapContent(mapContent); Rectangle rectangle = new Rectangle(imageWidth, imageHeight); System.out.println(rectangle.height + " " + rectangle.width); BufferedImage bufferedImage; bufferedImage = new BufferedImage( rectangle.width, rectangle.height, //BufferedImage.TYPE_INT_RGB); BufferedImage.TYPE_INT_ARGB); Graphics2D graphics2D = bufferedImage.createGraphics(); // Set white background //graphics2D.setComposite(AlphaComposite.Clear); graphics2D.setBackground(Color.white); graphics2D.fillRect(0, 0, imageWidth, imageHeight); //graphics2D.setComposite(AlphaComposite.getInstance(AlphaComposite.SRC_OVER, 0)); // Render onto background //graphics2D.setComposite(AlphaComposite.Src); renderer.paint(graphics2D, rectangle, mapContent.getMaxBounds()); //wait(this, 4000); try { ImageIO.write( bufferedImage, outputType, outputImageFile); } catch (IOException ex) { Logger.getLogger(Select.class.getName()).log(Level.SEVERE, null, ex); } //wait(this, 4000); graphics2D.dispose(); } Thanks again, Andy ________________________________________ From: Alex Davies-Moore [a...@devork.com] Sent: 11 November 2014 13:43 To: geotools-gt2-users@lists.sourceforge.net Subject: Re: [Geotools-gt2-users] Output to image file format Hi Andy, Don't know if it helps or not, but if you look at the class https://bitbucket.org/mapsum/maprocket-tileserver/src/ee045bb240e24091d7ecfeef1a151eca16c3ed4e/src/main/java/com/mapsum/maprocket/gis/wms/WMSHttpHandler.java?at=master I use it to output an image file as part of a WMS request. The bit you want is probably everything from line 106 - 154. HTH Alex [http://www.devork.com/devork.logo.colour.side.png] Alex Davies-Moore / Developer 25 Wayman Road / Corfe Mullen / Dorset / BH21 3PL / UK P: +44 (0) 1202 697 394 / M: +44 (0) 7961 629 399 / T: @schoenobates Devork Limited is a limited company registered in England and Wales Registered number: 07699891 Registered office: 25 Wayman Road, Corfe Mullen, Dorset, BH21 3PL This message and any files transmitted with it are the property of Devork Ltd, are confidential, and are intended solely for the use of the person or entity to whom this e-mail is addressed. If you are not one of the named recipient(s) or otherwise have reason to believe that you have received this message in error, please contact the sender and delete this message immediately from your computer. Any other use, retention, dissemination, forwarding, printing, or copying of this e-mail is strictly prohibited. On 11 November 2014 12:30, Andy Turner <a.g.d.tur...@leeds.ac.uk<mailto:a.g.d.tur...@leeds.ac.uk>> wrote: Hi geotools-gt2-users, Recently I started using GeoTools again for a project where I want to automate the production of a set of maps. I've managed to get some maps displayed on screen using a JMapFrame. Does anyone know a good way to write the display out to an image file? Thanks for your help, Andy http://www.geog.leeds.ac.uk/people/a.turner/index.html ------------------------------------------------------------------------------ Comprehensive Server Monitoring with Site24x7. Monitor 10 servers for $9/Month. Get alerted through email, SMS, voice calls or mobile push notifications. Take corrective actions from your mobile device. http://pubads.g.doubleclick.net/gampad/clk?id=154624111&iu=/4140/ostg.clktrk _______________________________________________ GeoTools-GT2-Users mailing list GeoTools-GT2-Users@lists.sourceforge.net<mailto:GeoTools-GT2-Users@lists.sourceforge.net> https://lists.sourceforge.net/lists/listinfo/geotools-gt2-users ------------------------------------------------------------------------------ Comprehensive Server Monitoring with Site24x7. Monitor 10 servers for $9/Month. Get alerted through email, SMS, voice calls or mobile push notifications. Take corrective actions from your mobile device. http://pubads.g.doubleclick.net/gampad/clk?id=154624111&iu=/4140/ostg.clktrk _______________________________________________ GeoTools-GT2-Users mailing list GeoTools-GT2-Users@lists.sourceforge.net https://lists.sourceforge.net/lists/listinfo/geotools-gt2-users