如何在 java 中正确加载 BufferedImage?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/601274/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-11 16:42:29 来源:igfitidea点击:
How do I properly load a BufferedImage in java?
提问by William
Okay, so I've been trying to load a BufferedImage using this code:
好的,所以我一直在尝试使用以下代码加载 BufferedImage:
URL url = this.getClass().getResource("test.png");
BufferedImage img = (BufferedImage) Toolkit.getDefaultToolkit().getImage(url);
This gives me a type cast error when I run it though, so how do I properly load a BufferedImage?
但是,当我运行它时,这给了我一个类型转换错误,那么我如何正确加载 BufferedImage?
采纳答案by Zach Scrivena
回答by Usman
BufferedImage img = null;
try {
img = ImageIO.read(new File("D:\work\files\logo.jpg"));
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}