How to load and write an image in Java?
Written on September 29, 2009 at 7:09 am by
mkyong
If you want to load an image from your drive , javax.imageio.ImageIO lets you do it.
BufferedImage originalImage = ImageIO.read(new File("c:\\image\\mypic.jpg"));
If you want to write an image to your drive , javax.imageio.ImageIO lets you do it.
ImageIO.write(originalImage, "jpg", new File("c:\\image\\mypic_new.jpg"));
Example
This class will load an image from “c:\\image\\mypic.jpg” and write it to an new image located at “c:\\image\\mypic_new.jpg”
import java.awt.image.BufferedImage; import java.io.File; import java.io.IOException; import javax.imageio.ImageIO; /* * @author mkyong * */ public class ImageTest { public static void main(String [] args){ try{ BufferedImage originalImage = ImageIO.read(new File("c:\\image\\mypic.jpg")); ImageIO.write(originalImage, "jpg", new File("c:\\image\\mypic_new.jpg")); }catch(IOException e){ System.out.println(e.getMessage()); } } }
Oracle Magazine (Free)
Oracle Magazine contains technology strategy articles, sample code, tips, Oracle and partner news, how to articles for developers and DBAs, and more. Oracle (NASDAQ: ORCL) is the world\'s largest enterprise software company.
Publisher : Oracle Corporation



You could also use Sanselan which is part of the Apache Commons project. It solves the task to load/store images in an easier way.
Bye,
Daniel