Georgia Ingham Georgia is a Java Developer. She has spoken at lots of conferences. Her hobbies include reading, completing puzzle books and cycling.

How to write out BMP images in Java

1 min read

In this article I will show you how to write BMP images in Java using ImageIO, and JDeli.

We previously used ImageIO to write BMPfiles, but over time became increasingly dissatisfied as we discovered more and more issues. Eventually we wrote our own BMP Encoder which fixes those issues and is now available as part of JDeli.

What is a BMP file?

BMP ImageA BMP is an image file format, commonly known as a bitmap image, with the extension .bmp or .dib. It was originally created by Microsoft for computers running Windows, and it specifies the color of the pixels in a way that allows them to be displayed on any device.

 

ImageIO

ImageIO.write(bufferedImage, "BMP", new File("/path/to/outputFile.bmp"));

Javadoc  (included in JDK)

JDeli

Javadoc and Download

You can easily replace ImageIO (and get much better support for BMP files) by just changing ImageIO.write to JDeli.write.

//Write Image (can also be OutputStream)
File myNewBmpFile=new File("ImageAsBmp.bmp");
JDeli.write(myBufferedImage, "bmp", myNewBmpFile);

Or you can use the JDeli BmpEncoder directly:

BmpEncoder encoder = new BmpEncoder();
encoder.write(bufferedImage, outputStream);
 

The JDeli library supports Bilevel, Grayscale and RGB images.

Why use JDeli?

JDeli offers a range of advantages over ImageIO and alternatives, including:

  • prevents heap related JVM crashes
  • implements unsupported image formats
  • reduce output file size
  • improve read/write performance
  • supports threading
  • superior image scaling algorithms

Learn more about JDeli, or download to try it yourself.

This article is part of our series on reading and writing image files in Java.

Georgia Ingham Georgia is a Java Developer. She has spoken at lots of conferences. Her hobbies include reading, completing puzzle books and cycling.

Leave a Reply

Your email address will not be published. Required fields are marked *

IDRsolutions Ltd 2019. All rights reserved.