Extract image from a JPanel

0

Good morning, I have a problem, and I do not know how to get the image of a jpanel and transform it to an Image object, I tried with the following code:

 private static BufferedImage createImage(JPanel panel) {
    int w = panel.getWidth();
    int h = panel.getHeight();
    BufferedImage bi = new BufferedImage(w, h, BufferedImage.TYPE_INT_RGB);
    Graphics2D g = bi.createGraphics();
    panel.print(g);
    return bi;
 }

But of course I do not know how to convert from BufferedImage to Image. Someone can help me out, it would be very helpful.

Thanks in advance.

    
asked by barraca96 05.04.2017 в 10:32
source

1 answer

1

Try the following, to convert your BufferedImage to image.

File salida= new File("imagen.png");
ImageIO.write(bi , "png", salida);
    
answered by 05.04.2017 в 14:33