Saturday, March 1, 2008

Convert a PNG to a BMP in Java

This tutorial shows how to use the ImageIO API to convert a PNG to a BMP image in Java. The ImageIO API provides methods to read the source image and to write the image in the new file format.

To read the image, simply provide the ImageIO.read() method a File object for the source image. This will return a BufferedImage.

//Create file for the source
File input = new File("c:/temp/image.png");

//Read the file to a BufferedImage
BufferedImage image = ImageIO.read(input);

Once you have the BufferedImage, you can write the image as a BMP. You will need to create a File object for the destination image. When calling the write() method, specify the type string as "bmp".

//Create a file for the output
File output = new File("c:/temp/image.bmp");

//Write the image to the destination as a BMP
ImageIO.write(image, "bmp", output);

0 comments: