Convert BMP File to PNG in Java Example - Part 2

In the first part of this series, we examined how to convert BMP to PNG using JMagick Java library. In this example,  we will discuss how to perform the same conversion using ImageIO Java API.

How to convert BMP to PNG using ImageIO – Java Example


Using javax.imageio.ImageIO class, you can easily convert a BMP image file to PNG in Java. You can read the input image into BufferedImage object.  Once the image is read successfully, you can convert it into PNG by using the built in methods available in ImageIO class.

Complete Program

The complete code example to convert BMP to PNG using ImageIO library is provided below:

import java.awt.image.BufferedImage; //This class will read the incoming BMP image
import javax.imageio.ImageIO; //This class will convert BMP to PNG
import; //to read input and write output images
public class imageio_bmp_png {
  public static void main(String[] args) throws Exception {
      BufferedImage input_image = null; 
      input_image = File("Convert_to_PNG.bmp")); //read bmp into input_image object
      File outputfile = new File("imageio_png_output.png"); //create new outputfile object
      ImageIO.write(input_image, "PNG", outputfile); //write PNG output to file 

There is a definite difference in PNG output file size when using ImageIO to convert image formats. The file size is in this case is provided below: (88 Kb against 123 Kb when using JMagick, refer Part 1 of the tutorial below for JMagick example)

ImageIO Converted PNG Image Output - Example
ImageIO Converted PNG Image Output - Example
Download Source Code

You can download the source code for this example from the link below

ImageIO looks definitely better at this stage looking at the file size. We are not done yet. In the next part, we will see how to do the same conversion using Java Advanced Imaging API. (JAI)
This Series:  How to convert BMP file to PNG format in Java?

Keywords: JAI BMP to PNG, JMagick BMP to PNG, ImageIO BMP to PNG, Java Image conversion Example, Java BMP to PNG Example

Tutorial Parts:
Part 1 – Convert BMP to PNG in Java using JMagick
Part 2 – Convert BMP to PNG in Java using ImageIO
Part 3 -  Convert BMP to PNG in Java using JAI

1 comment:

  1. Thank you for the tutorial. Very useful