我有一个仅包含一个图标的 JLabel,我可以使用 label1.getIcon() 获取该图标,但我不知道如何将该图标转换为 BufferedImage。仅供引用,我不是在谈论 ImageIcon,只是在谈论 Icon。另外,我在How to convert Icon from JLabel into BufferedImage?看到了这个问题,但我似乎无法弄清楚。
一如既往,非常感谢任何示例或解释。 谢谢!
最佳答案
你可以试试这个。
// 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();
关于java - 如何将 JLabel.getIcon() 转换为 BufferedImage,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14228433/