Java Write To Image File Example

To Write Image in different format you may use javax.imageio.ImageIO class. To compress the image in different format you need to do the following

Java Write To Image File Example

To Write Image in different format you may use javax.imageio.ImageIO class. To compress the image in different format you need to do the following

Java Write To Image File Example

Java Write To Image File Example

To Write Image in different format you may use javax.imageio.ImageIO class. To compress the image in different format you need to do the following

At first create an instance of your original Image file as

/* Create new Image File Object */
File origionalImage = new File("myImage.jpg");

Create an ImageBuffer object of Origional Image as

/* Read the image */
BufferedImage bufferedImage = ImageIO.read(origionalImage);

Create an instance of Different Image Type

/* Creating an Instance for different Image types */
File myJpegImage  = new File("myJpegImage.jpg");
File myBmpImage = new File("myBmpImage.bmp");
File myGifImage = new File("myGifImage.gif");
File myPngImage = new File("myPngImage.png");

Finally write the Images using ImageIO's  write() method in different format.

ImageIO.write(bufferedImage, "jpg", myJpegImage);
ImageIO.write(bufferedImage, "bmp", myBmpImage);
ImageIO.write(bufferedImage, "gif", myGifImage);
ImageIO.write(bufferedImage, "png", myPngImage);  

An example of above file is given below

WriteImageFileExample.java

import javax.imageio.ImageIO;
import java.io.File;
import java.io.IOException;
import java.awt.image.BufferedImage;

public class WriteImageFileExample {    

    public static void main( String[] args ) throws IOException{      
		
		/* Create new Image File Object */
		File origionalImage = new File("myImage.jpg");

		/* Read the image */
		BufferedImage bufferedImage = ImageIO.read(origionalImage);

		/* Creating an Instance for different Image types */
		File myJpegImage  = new File("myJpegImage.jpg");
		File myBmpImage = new File("myBmpImage.bmp");
		File myGifImage = new File("myGifImage.gif");
		File myPngImage = new File("myPngImage.png");


		/* Writing different images using write() method of ImageIO class that takes three parameters
		   Origional Image Buffer, type of image and new Image file which have to change */
		ImageIO.write(bufferedImage, "jpg", myJpegImage);
		ImageIO.write(bufferedImage, "bmp", myBmpImage);
		ImageIO.write(bufferedImage, "gif", myGifImage);
		ImageIO.write(bufferedImage, "png", myPngImage);       
    }
}

Note:- please put a image named myImage.jpg in the same directory where your class

Download Select Source Code