ImageIO.write bmp does not work

后端 未结 3 1418
余生分开走
余生分开走 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

    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());
    }
    

提交回复
热议问题