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)
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.
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.