8

I have a BufferedImage object and I want to encode it to the BMP format and save it to disk.

How do I do this?

In JPEG it's ok:

BufferedImage img; //here is an image ready to be recorded into the hard disk
FileOutputStream fout = new FileOutputStream("image.jpg");

JPEGImageEncoder jencoder = JPEGCodec.createJPEGEncoder(fout);
JPEGEncodeParam enParam = jencoder.getDefaultJPEGEncodeParam(img);

enParam.setQuality(1.0F, true);
jencoder.setJPEGEncodeParam(enParam);
jencoder.encode(img);

fout.close();
mutlei
  • 145
  • 1
  • 8
Eduardo Abreu
  • 245
  • 2
  • 3
  • 9

2 Answers2

11

Use ImageIO -

ImageIO.write(img, "BMP", new File("filename.bmp"))
Anand S Kumar
  • 88,551
  • 18
  • 188
  • 176
Marc
  • 3,550
  • 22
  • 28
  • 2
    Make sure that `BufferedImage` is `BufferedImage.TYPE_INT_RGB` and not `BufferedImage.TYPE_INT_ARGB` – c0der Jan 14 '21 at 09:44
7

Something like this should do:

ImageIO.write(image, "BMP", new File("filename.bmp"));

where image is the BufferedImage you want to encode.

Faisal Feroz
  • 12,458
  • 4
  • 40
  • 51
  • 1
    Make sure that `BufferedImage` is `BufferedImage.TYPE_INT_RGB` and not `BufferedImage.TYPE_INT_ARGB` – c0der Jan 14 '21 at 09:44