Convert ImageIcon to Image or ImageIcon to BufferedImage

0

I'm having trouble finding ways to convert ImageIcon to Image or if possible ImageIcon to BufferedImage in order to save it to the database. I gave some searches on the internet but the information found is a bit confusing!

    
asked by anonymous 27.11.2017 / 05:18

1 answer

2

To get a Image instance of a Imageicon type, simply use the getImage() :

Image img = seuIcon.getImage();

Simple like this;).

And to convert to BufferedImage , try as suggested in this SOen response :

BufferedImage bi = new BufferedImage(icon.getIconWidth(), icon.getIconHeight(), BufferedImage.TYPE_INT_RGB);
Graphics g = bi.createGraphics();
// paint the Icon to the BufferedImage.
icon.paintIcon(null, g, 0,0);
g.dispose();

The ideal would be to save only the path of the image and itself in some directory of the server, but if you want to continue and save in the database, usually the format is blob, but you do not need to convert to bufferedimage or image.     

27.11.2017 / 10:32