|
How to read an Image from a file, inputStream, or URL |
|
|
This Java tip illustrates a method of reading an Image from a file, inputStream, or URL.
This tip also includes displaying of an image on the screen. Further, javax.imageio
package is used to read an image from a file. This example works only above J2SE 1.4.
Image image = null;
try {
// Read from a file
File sourceimage = new File("source.gif");
image = ImageIO.read(sourceimage);
// Read from an input stream
InputStream is = new BufferedInputStream(
new FileInputStream("source.gif"));
image = ImageIO.read(is);
// Read from a URL
URL url = new URL("http://java-tips.org/source.gif");
image = ImageIO.read(url);
} catch (IOException e) {
}
// Use a label to display the image
JFrame frame = new JFrame();
JLabel label = new JLabel(new ImageIcon(image));
frame.getContentPane().add(label, BorderLayout.CENTER);
frame.pack();
frame.setVisible(true);
|
Related Tips
|
Page 1 of 0 ( 0 comments )
You can share your information about this topic using the form below!
Please do not post your questions with this form! Thanks.