1

Iam trying to convert a JFrame to a Bitmap. I know it is possible to convert it to a png-File, but Iam unable to convert it to a bmp-File. My code:

public class ToImage {

    public static void main(String[] args) throws IOException {
          JFrame jf = new JFrame();
          jf.add(new JButton("Example"));
          jf.setVisible(true);
          jf.pack();

          System.out.println(toImage(jf, "png"));
          System.out.println(toImage(jf, "bmp"));
    }

    private static boolean toImage(Container container, String fileEnding) throws IOException {
        int w = container.getWidth();
        int h = container.getHeight();
        BufferedImage bi = new BufferedImage(w, h, BufferedImage.TYPE_INT_ARGB);
        container.paint(bi.createGraphics());

        /* Save file in documents folder */
        File file = new File(new JFileChooser().getFileSystemView().getDefaultDirectory().toString() + "/example." + fileEnding);
        return ImageIO.write(bi, fileEnding, file);
    }
}

The output is:

true
false

The .png-File exists and looks correct.

Why does ImageIO.write return false when I try to save my JFrame as a bitmap? Is there a way to save my JFrame as a Bitmap?

Thank you

Moritz Schmidt
  • 2,635
  • 3
  • 27
  • 51

0 Answers0