public static byte[] encode(BufferedImage image, String format,
                                boolean encodeAlpha) throws IOException {
        ImageEncoder imageEncoder 
            = ImageEncoderFactory.newInstance(format, encodeAlpha);
        return imageEncoder.encode(image);