Previously, we have written about how to read BMP files as images in Java. This time around we wanted to highlight how you can write out BMP images in Java.
What is a BMP file?
A 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.
There are two different ways you can write out BMP files in Java. One way is to use ImageIO, and an alternative is to use our JDeli image library.
ImageIO.write(bufferedImage, "BMP", new File("/path/to/outputFile.bmp"));
Javadoc (included in JDK)
BmpEncoder encoder = new BmpEncoder(); encoder.write(bufferedImage, outputStream);
Alternatively, JDeli also has a static write method. This makes it easy to swap out ImageIO for JDeli in your code:
JDeli.write(bufferedImage, JDeli.OutputFormat.BMP, new File("/path/to/outputFile.bmp"));
The JDeli library supports Bilevel, Grayscale and RGB images.
If you found this guide useful, you can find more image tutorials in our how to series on how to read and write various Image formats.