import java.awt.Color; import java.awt.Graphics2D; import java.awt.image.BufferedImage; import java.io.ByteArrayOutputStream; import java.io.IOException; import javax.imageio.ImageIO; public class ImageIOTest { public static void main(String[] args) throws IOException { BufferedImage buf; buf = new BufferedImage(128, 128, BufferedImage.TYPE_INT_BGR); Graphics2D g = buf.createGraphics(); g.setColor(Color.MAGENTA); g.drawRect(0, 0, 100, 60); ByteArrayOutputStream bo = new ByteArrayOutputStream(); ImageIO.write(buf, "PNG", bo); byte[] bytes = bo.toByteArray(); } }