Here you can find the source of toByteArray(BufferedImage image, String formatName)
public static byte[] toByteArray(BufferedImage image, String formatName) throws IOException
//package com.java2s; import java.awt.image.BufferedImage; import java.io.ByteArrayOutputStream; import java.io.IOException; import javax.imageio.ImageIO; public class Main { public static byte[] toByteArray(BufferedImage image, String formatName) throws IOException { ByteArrayOutputStream os = new ByteArrayOutputStream(); ImageIO.write(image, formatName, os); byte[] data = os.toByteArray(); os.close();//from w w w .j ava 2s . c o m return data; } }