How to load and write an image in Java?

In this tutorial, we show you how to use “javax.imageio.ImageIO” to load and write image.

To load image :


	BufferedImage originalImage = ImageIO.read(new File("c:\\image\\mypic.jpg"));

To write image :


	ImageIO.write(originalImage, "jpg", new File("c:\\image\\mypic_new.jpg")); 

Example

Full example to 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;

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());
		}

	}

}

About the Author

author image
mkyong
Founder of Mkyong.com, love Java and open source stuff. Follow him on Twitter. If you like my tutorials, consider make a donation to these charities.

Comments

avatar
3 Comment threads
0 Thread replies
0 Followers
 
Most reacted comment
Hottest comment thread
3 Comment authors
Sumit badayaHarikaDaniel Manzke Recent comment authors
newest oldest most voted
Sumit badaya
Guest
Sumit badaya

can you please provide simple and easy to understand code to upload image in database and retrieve image from database in jsp ?

database is SQL SERVER 2005..thanks in advance.

Harika
Guest
Harika

Can U Provide Me Java Code for Converting Jpeg Image to Tiff Image

Daniel Manzke
Guest
Daniel Manzke

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