Reading an Image from a File, InputStream, or URL

This example demonstrates how to use the javax.imageio package to read an image from a file, input stream, or URL. The example also demonstrates how to display the image on the screen.

GIF, PNG, and JPEG formats are supported by the javax.imageio package by default.

This example works only in J2SE 1.4. For a method that works since J2SE 1.3, see Reading an Image or Icon from a File.

Image image = null;
try {
    // Read from a file
    File file = new File("image.gif");
    image = ImageIO.read(file);

    // Read from an input stream
    InputStream is = new BufferedInputStream(
        new FileInputStream("image.gif"));
    image = ImageIO.read(is);

    // Read from a URL
    URL url = new URL("http://hostname.com/image.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);

Comments

5 Mar 2010 - 12:59am by Anonymous

thanx a lot it really helped me

24 Mar 2010 - 5:27am by Anonymous (not verified)

thanx... would you by any chance help me to make the image resize according to the size of the frame?

12 Jul 2010 - 7:57am by Anonymous (not verified)

new FileInputStream("image.gif")); results in FileNotFound exception. Help?

26 Jul 2010 - 2:35pm by Art (not verified)

To the poster on Jul 12, image.gif needs to be located in your root directory. Otherwise just specify path to the image: new FileInputStream("/images/navigation/back.gif")); or whatever the path is relative to your root directory.

16 Sep 2010 - 7:59am by Dariel (not verified)

Hi everyone. I'm wondering how can I load an image from any directory of the disc, not from the root of the project. I'm working whit JDeveloper 11.1.1.2.0. With the image component work only if the image is in the root. Thanks in advance, any help will be fine.

13 Nov 2010 - 3:37pm by Srikkanth (not verified)

Is it possible to parse an image which contains text content and retrieve the text contenet separately ? In otherwords suppose I have a scanned copy of my typed assignment.can i get the text content from the scanned image ?

30 Jan 2011 - 10:13am by Jerrycheng (not verified)

It is a really useful web site for me , thanks.

13 Feb 2011 - 9:47am by Kyle Koder (not verified)

Just what I was looking for. :)

16 Feb 2011 - 6:51am by Anonymous1 (not verified)

Thanks man! It's very interesting shit!

11 May 2011 - 11:08am by durgesh (not verified)

Thanks a lot!

19 Jun 2011 - 9:43pm by saratchandra3@yahoo.co.in (not verified)

Hi Friends I am writing an image to the jsp page using
OutputStream os = response.getOutputStream();
ImageIO.write(image, "jpeg", os);
Where image is my buffered image, i am getting "javax.imageio.IIOException: Maximum supported image dimension is 65500 pixels" error.
Is there a way to fix this with out changing the actual image dimensions ?

19 Jun 2011 - 11:39pm by vaishali (not verified)

Can i read DWF file with this ImageIO.read() function

2 Aug 2011 - 1:53pm by Anonymous (not verified)

Hi Very interesting to me this code portion, I have a question, sorry my basic knowledge are, what is the sustancial difference between "new File()" and "new FileInputStream()".
Regards.

4 Aug 2011 - 10:04pm by Anonymous (not verified)

how to retrieve the multiple images from mysql database in swing

18 Oct 2011 - 2:28am by Anonymous (not verified)

that's great!

28 Oct 2011 - 3:20am by VieHai (not verified)

Thanks for post and comment there are great @@

17 Nov 2011 - 6:17am by Pavel (not verified)

Please help. How to set cookies for imaging?

20 Nov 2011 - 11:20am by Anonymous (not verified)

this is very nice!

20 Nov 2011 - 11:21am by Anonymous (not verified)

this is very nice!

20 Nov 2011 - 11:24am by Anonymous (not verified)

nice

20 Nov 2011 - 11:25am by Anonymous (not verified)

this is very nice!

28 Nov 2011 - 2:47pm by Anonymous (not verified)

this is very nice!

28 Nov 2011 - 2:57pm by Anonymous (not verified)

this is very nice!

11 Dec 2011 - 6:24pm by Anonymous (not verified)

God I love this site: all the examples are so simple - exactly what I want.

15 Feb 2012 - 11:22am by Direct Man (not verified)

Amazing work, THAAAAAAAAAAAANKS

6 Mar 2012 - 4:58am by adamo651 (not verified)

Thank you :) this is very nice.

Post a comment

More information about formatting options

CAPTCHA
This question is for testing whether you are a human visitor and to prevent automated spam submissions.
Image CAPTCHA
Enter the characters shown in the image. Ignore spaces and be careful about upper and lower case.