How to write an image to file – Java ImageIO

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 :

About the Author

mkyong
Founder of Mkyong.com and HostingCompass.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 make a donation to these charities.

Comments

  • ram

    Thanks,

    but it is not woking for me. please help me, i am getting error shown in below.

    javax.imageio.IIOException: Can’t get input stream from URL!
    at javax.imageio.ImageIO.read(Unknown Source)
    at Image.main(Image.java:17)

    Caused by: java.net.ConnectException: Connection refused: connect
    at java.net.DualStackPlainSocketImpl.connect0(Native Method)
    at java.net.DualStackPlainSocketImpl.socketConnect(Unknown Source)
    at java.net.AbstractPlainSocketImpl.doConnect(Unknown Source)

    thanks for your understand

  • Shinu Varghese

    Hi,
    How do you incorporate scribling of text onto an image in java…..

  • Shinu Varghese

    I have a java project in which i have to scribble text using a writing pad onto an existing image in a label…
    please help me on the above case……

  • http://www.univ_m'sila.com walid

    After converting the image to 0101 and encrypted, I can not bring it back as an image please help

  • http://www.mv3solutions.com Amit

    I am trying to upload a file path through action class to the server from local system.I have done it statically
    if(customerBean.getUploadFileFileName() != null)
    {
    String filePath = “E:\\photos\\customer\\”;
    String photoPath = “E:/photos/customer/”+customerBean.getUploadFileFileName();

    File fileToCreate = new File(filePath, customerBean.getUploadFileFileName());
    if(!fileToCreate.exists())
    {
    setErrormessage(“Please check if Uploaded Photo exists in the Customer Folder”);
    return “failure”;
    }
    status = custDAO.insertUser(customerBean,photoPath);
    }

    Could you please help me out with a working code where i can upload images dynamically on the server and store the image path in server datatbase.

    Thanks,
    Amit.

  • ramesh

    Respected sir,
    iam doing project on visual cryptography
    i.e: based on two images generate another image. here one image is orinal image with text. another is indecipheralble form. generate another image is also indecipherable form.both are combine whatever the text in original image. that will be visible. plz help me sir. plz

  • ramesh

    based on two images generate another image here one image is orinal image with text another is indecipheralble form generate another image is also indecipherable form. plz help me sir

  • ramesh

    sir plz help me iam doing project on visual cryptography.i want code in java

  • Fernando

    When i user an image *.png and try to get the *.bmp the file generated is a 0 bytes file.

    May be you could help me to know why is this happening??

    Thanks

  • http://itsallabouttechnology.com tom costa

    Above code works on my machine , thanks for sharing

  • Jason

    Exception in thread “main” java.lang.IllegalArgumentException: im == null!

    Below is the code :

     
    BufferedImage bImageFromConvert = ImageIO.read(new ByteArrayInputStream(binaryStream));		
    ImageIO.write(bImageFromConvert, "jpg", new File("D:\\"+ fileName +".jpg"));
    • swastik

      Hi,
      For Java its working, while I try to put the code for a managed bean in JSF in NetBean IDE the file is created under build\web directory instead of web. Can you please help on this

      FacesContext con = FacesContext.getCurrentInstance();
      ExternalContext contx = con.getExternalContext();
      String path = contx.getRealPath(“/”);
      URL url = new URL(“http://www.mkyong.com/image/mypic.jpg”);
      image = ImageIO.read(url);

      ImageIO.write(image, “jpg”,new File(path +”out.jpg”));

      Thanks