英文:
How to convert File type to Image?
问题
我想将它转换为java.awt
中的Image
。我应该如何做到这一点?
英文:
I've got this code:
File obraz = new File("C:\\Users\\ender\\Pictures\\logo.jpg");
I would like to convert it into an Image
from java.awt
. How should I do that?
答案1
得分: 4
你可以这样做:
try {
File obraz = new File("C:\\Users\\ender\\Pictures\\logo.jpg");
Image image = ImageIO.read(obraz);
} catch (IOException ex) {
ex.printStackTrace();
}
使用 ImageIO 帮助类。
英文:
You can do this like this:
try {
File obraz = new File("C:\\Users\\ender\\Pictures\\logo.jpg");
Image image = ImageIO.read(obraz);
} catch (IOException ex) {
ex.printStackTrace();
}
Using ImageIO helper class.
答案2
得分: 1
Image img = null;
try {
img = ImageIO.read(new File("C:\Users\ender\Pictures\logo.jpg"));
}
catch (IOException e) {}
英文:
Try this:
Image img = null;
try {
img = ImageIO.read(new File("C:\\Users\\ender\\Pictures\\logo.jpg"));
}
catch (IOException e) {}
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论