Question

I want to save a BufferedImage (named "result"):

boolean bres = ImageIO.write(result, ".png", new File(saveP)); 

But bres is always null.

This

ImageIO.getWriterFormatNames()

returns that:

jpg BMP bmp JPG jpeg wbmp png JPEG PNG WBMP GIF gif

I should be able to save as an png.

And the type of the BufferedImage is "2"

BufferedImage@137695c: type = 2 DirectColorModel: rmask=ff0000 gmask=ff00 bmask=ff amask=ff000000 IntegerInterleavedRaster: width = 720 height = 576 #Bands = 4 xOff = 0 yOff = 0 dataOffset[0] 0

Type 2 is "ARGB".

Why i can't save the BufferedImage?

EDIT: saveP = "ex000567.png"

Was it helpful?

Solution

private static void savePNG( final BufferedImage image, final String path ){
        try {
            RenderedImage rendImage = image;
            ImageIO.write(rendImage, "PNG", new File(path));
        } catch ( IOException e) {
            e.printStackTrace();
        }
    }

Test this function. This one works for me.

The important change is that the second parameter to ImageIO.write is changed from ".png" to "PNG" (lowercase "png" would also work), refer to the output of ImageIO.getWriterFormatNames() for valid names.

OTHER TIPS

FormateName should be "png" not ".png"

For anyone who experiences this issue with JPEG files, please make sure that your buffered image does not have an alpha channel. Otherwise, com.sun.imageio.plugins.jpeg.JPEGImageWriterSpi#canEncodeImage will reject your image.

For example, instead of BufferedImage.TYPE_INT_ARGB use BufferedImage.TYPE_INT_RGB or something else that does not have an alpha channel.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top