如何将 BufferedImage 转换为 ImageIcon?
我找不到任何关于此的文档。
使用构造函数:ImageIcon(Image image)
,BufferedImage
扩展Image
。
BufferedImage
extends Image
,所以很简单:
new ImageIcon(myBufferedImage);
File img = new File("C:\\..\\image.jpg");
BufferedImage bufferedImage = ImageIO.read(img);
ImageIcon imageIcon = new ImageIcon(bufferedImage);
your bufferimage;
JLabel photo = new JLabel("Photo");
photo.setIcon(new ImageIcon(bufferimage));
这是一个简单的转换器:
//Your icon
private ImageIcon icon;
//Your image
private BufferedImage image;
public void bufferImageTocon(){
File file = new File("test.jpg");
try {
this.image = ImageIO.read(file);
} catch (IOException ex) {
ex.printStackTrace();
}
icon = new ImageIcon(image);
}
仅使用构造函数来制作新图标,将缓冲图像设置为参数。