Reputation: 27074
Does anyone have a way of storing a BufferedImage
with transparency as BMP in Java? Preferably using the ImageIO
API.
For some reason I can't write a BMP in ARGB (BGRA) format, even though the BMP has supported alpha channel since, at least, Win95. I can, however, easily write the same image as a PNG. It also works fine storing an image without alpha, like TYPE_INT_RGB
or TYPE_3BYTE_BGR
.
Consider the following code snippet:
public static void main(String[] args) throws IOException {
if (!ImageIO.write(new BufferedImage(300, 200, BufferedImage.TYPE_INT_ARGB), "BMP", new File("foo.bmp"))) {
System.err.println("Couldn't write BMP!");
}
if (!ImageIO.write(new BufferedImage(300, 200, BufferedImage.TYPE_INT_ARGB), "PNG", new File("foo.png"))) {
System.err.println("Couldn't write PNG!");
}
}
Output:
Couldn't write BMP!
Upvotes: 1
Views: 680
Reputation: 9543
public boolean canEncodeImage(ImageTypeSpecifier type) {
...
if (!(numBands == 1 || numBands == 3))
return false;
I'm afraid Java doesn't support 32 bit BMPs. You'd have to write your own or find an existing library (ImageJ comes to mind although I don't know if it supports BMP). Wikipedia is very elaborate on the file format and it looks quite easy, even giving a complete field-by-field example of a 32-bit BMP.
Upvotes: 2