The “javax.imageio.ImageIO” is a handy class to read and write image into local system. In this example, we show you how to use “ImageIO” to read an image from an URL and write it into three file formats :

  1. “jpg”
  2. “gif”
  3. “png”

Full example to demonstrate the ImageIO usage.

package com.mkyong.image;
 
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;
import java.net.URL;
import javax.imageio.ImageIO;
 
public class WriteImage 
{	
    public static void main( String[] args )
    {
    	BufferedImage image = null;
        try {
 
            URL url = new URL("http://www.mkyong.com/image/mypic.jpg");
            image = ImageIO.read(url);
 
            ImageIO.write(image, "jpg",new File("C:\\out.jpg"));
            ImageIO.write(image, "gif",new File("C:\\out.gif"));
            ImageIO.write(image, "png",new File("C:\\out.png"));
 
        } catch (IOException e) {
        	e.printStackTrace();
        }
        System.out.println("Done");
    }
}

Reference

  1. http://java.sun.com/j2se/1.4.2/docs/api/javax/imageio/ImageIO.html
Tags :
Founder of Mkyong.com, love Java and open source stuff. Follow him on Twitter, or befriend him on Facebook or Google Plus. If you like my tutorials, consider making a donation to the charity, thanks.

Related Posts

Popular Posts