How to write an image to file – Java ImageIO
Published: January 17, 2010 , Updated: May 10, 2011 , Author: mkyong
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 :
- “jpg”
- “gif”
- “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"); } }






Above code works on my machine , thanks for sharing
Exception in thread “main” java.lang.IllegalArgumentException: im == null!
Below is the code :