如何将文件类型转换为图像?

huangapple go评论71阅读模式
英文:

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) {}

Documentation

huangapple
  • 本文由 发表于 2020年8月12日 23:24:24
  • 转载请务必保留本文链接:https://go.coder-hub.com/63379747.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定