ImageIO.write bmp does not work

后端 未结 3 1403
余生分开走
余生分开走 2020-12-05 19:50

I\'m trying to save an image in bmp format, but it doesn\'t create any file. If I use \"png\" instead, everything works fine. Any ideas?

//This works fine:
I         


        
相关标签:
3条回答
  • 2020-12-05 20:18

    As @bincob says, if write returns false, you can redraw the source image like this

    BufferedImage newBufferedImage = new BufferedImage(bufferedImage.getWidth(), 
    bufferedImage.getHeight(), BufferedImage.TYPE_INT_RGB);
    newBufferedImage.createGraphics().drawImage(bufferedImage, 0, 0, Color.WHITE, null);
    

    And then you can write again.

    0 讨论(0)
  • 2020-12-05 20:18

    didn't try but I think the format should be "BMP" and not "bmp" actually. Please try with

    ImageIO.write(bi, "BMP", new File("D:\\MyImage.bmp"));
    

    and see what happens.

    We can't see how your bi is build.

    BufferedImage bufferedImage = new BufferedImage(w,h,BufferedImage.TYPE_INT_RGB);
    

    Is the encodingType is set properly ?

    I think your bi is corrupted, that's work perfectly for me.

    BufferedImage bi = new BufferedImage(50,50,BufferedImage.TYPE_INT_RGB);
    Graphics gd = bi.getGraphics();
    gd.drawRect(0, 0, 10, 10);      
    try {
        ImageIO.write(bi, "BMP", new File("C:\\test.bmp"));
        ImageIO.write(bi, "PNG", new File("C:\\test.png"));
    } catch (IOException e) {
        System.out.println("error "+e.getMessage());
    }
    
    0 讨论(0)
  • 2020-12-05 20:28

    I just finished debugging a similar problem and I thought I will present my reasoning here, although Jakob has gone ahead with the PNG format.

    First, always check the return value of ImageIO.write(...). It will return false if no appropriate writer can be found and that's what should have happened when Jakob tried writing it as a bitmap. This happens when the actual image format of the file does not match what is given in the 'format name' argument. No exception is thrown in this case. Check out the docs at http://docs.oracle.com/javase/7/docs/api/javax/imageio/ImageIO.html#write(java.awt.image.RenderedImage, java.lang.String, java.io.File)

    Second, check the image type of the BufferedImage object by using the BufferedImage#getType() method. Check out the possible return values at http://docs.oracle.com/javase/7/docs/api/java/awt/image/BufferedImage.html#getType(). For example, If you get the type as TYPE_INT_ARGB from your BufferedImage object (which represents a PNG with a alpha component) you wont have success using ImageIO.write(bi, "BMP", new File("D:\\test.bmp")) and the method would return false, even though you can see BMP/bmp in the list of entries obtained using ImageIO.getWriterFormatNames(). You might have to work on the encoding and transform your image to the desired format.

    Third, when facing such problems which can be a pain sometimes, it always helps to use an image editor such as GIMP to check out your image properties in detail.

    @Green arrow, a minor note... you can use either "bmp" or "BMP" as the image format value. The same applies for other formats as well. It does not matter.

    0 讨论(0)
提交回复
热议问题