简体   繁体   English

如何将JLabel.getIcon()转换为BufferedImage

[英]How to convert JLabel.getIcon() to BufferedImage

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. 我有一个只包含图标的JLabel,我可以使用label1.getIcon()获取Icon,但我无法弄清楚如何将该Icon转换为BufferedImage。 Just FYI, I'm not talking about ImageIcon, only Icon. 仅供参考,我不是在谈论ImageIcon,只是在谈论Icon。 Also, I have seen the question at How to convert Icon from JLabel into BufferedImage? 另外,我已经看到了如何将图标从JLabel转换为BufferedImage的问题? , but I can't seem to figure it out. ,但我似乎无法弄明白。

As always, any examples or explanation are much appreciated. 与往常一样,任何示例或解释都非常感谢。 Thanks! 谢谢!

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();

由于JLabel.getIcon()返回一个Icon,因此你想将Icon转换为bufferedImage。我认为你需要查看这个问题。你可以通过这种方式将图标转换为BufferedImage

关注此链接,因为它在我的评论中帮助了你。除此之外,我会建议你研究一下ImageIconIcon

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM