Next page | Contents page |

Load, draw & save images

	try
	{
		java.awt.image.BufferedImage bim =
			javax.imageio.ImageIO.read (new File ("myphoto.jpg"));
		g2.drawImage (bim, cx - bim.getWidth (this) / 2,
						cy - bim.getHeight (this) / 2, this);
		// "this" is a java.awt.image.ImageObserver, notified of progress
		javax.imageio.ImageIO.write (bim, "png",
						new File ("myphoto.png"));
	}
	catch (IOException ex) { ... }

javax.imageio only arrived in Java 1.4, from the Java Advanced Imaging (JAI) project. Before that there was a more complicated process using a MediaTracker. imageio is much more comprehensive, allowing metadata and thumbnails to be manipulated too. There are 3rd party readers/writers available as imageio plug-ins for various image formats.

Next page | Contents page |