BufferedImage bimage = ImageHelper.createImage(width, height);
Graphics2D g = bimage.createGraphics();
g.setColor(Color.white);
g.fillRect(0, 0, width, height);
legend.arrange(g,new RectangleConstraint(width,height));
legend.draw(g, new Rectangle(width, height));
ByteArrayOutputStream out = new ByteArrayOutputStream();
JPEGImageEncoder encoder = JPEGCodec.createJPEGEncoder(out);
JPEGEncodeParam param = encoder.getDefaultJPEGEncodeParam(bimage);
param.setQuality(1.0f, true);
encoder.encode(bimage, param);