Here you can find the source of toByteArray(BufferedImage image)
public static byte[] toByteArray(BufferedImage image) throws IOException
//package com.java2s; //License from project: Open Source License import javax.imageio.ImageIO; import java.awt.image.BufferedImage; import java.io.ByteArrayOutputStream; import java.io.IOException; public class Main { public static byte[] toByteArray(BufferedImage image) throws IOException { ByteArrayOutputStream outputStream = new ByteArrayOutputStream(); ImageIO.write(image, "png", outputStream); return outputStream.toByteArray(); }//www. ja v a 2 s. com }