2

I have a JLabel that contains only an icon, and I can get the Icon with label1.getIcon(), but I can't figure out how to convert that Icon into a BufferedImage. Just FYI, I'm not talking about ImageIcon, only Icon. Also, I have seen the question at How to convert Icon from JLabel into BufferedImage?, but I can't seem to figure it out.

As always, any examples or explanation are much appreciated. Thanks!

Community
  • 1
  • 1
iphonedev7
  • 295
  • 1
  • 7
  • 17

2 Answers2

3

You may try this.

// Get the icon
Icon ico = label1.getIcon();
// Create a buffered image
BufferedImage bimg = new BufferedImage(ico.getIconWidth(), ico.getIconHeight(),
                                       BufferedImage.TYPE_INT_RGB);
// Create the graphics context
Graphics g = bimg.createGraphics();
// Now paint the icon
ico.paintIcon(null, g, 0, 0);
g.dispose();
Sri Harsha Chilakapati
  • 11,744
  • 6
  • 50
  • 91
3

As JLabel.getIcon() returns a Icon so you want to convert the Icon to bufferedImage.I think you need to view this question.Here you can get the way through which you can convert a icon to BufferedImage

Community
  • 1
  • 1
Despicable
  • 3,797
  • 3
  • 24
  • 42