Your Computer Science Resource

HOW-TO: Save a BufferedImage to Disk

Recently, I showed you how to take a screenshot and display it in a JFrame using Java’s Robot class. What if you wanted to save the image to the disk as a GIF, JPG, or PNG? It’s actually really easy. Let’s find out how!

Suppose we have our image or screenshot saved in a BufferedImage named bi. It’s almost effortless to save it to the disk:

File f = new File("screenshot.gif");
ImageIO.write( bi, "gif", f );

The first line specifies the path and file name. Since we left out the path, this image will be saved in the root of our project folder. The second line writes the BufferedImage to the file using the specified format.

Image Format

To save the image as a JPEG, simply rename the file and the format String to “jpg”:

File f = new File("screenshot.jpg");
ImageIO.write( bi, "jpg", f );

To save the image as a PNG, simply rename the file and the format String to “png”:

File f = new File("screenshot.png");
ImageIO.write( bi, "png", f );

Notes On Image Formatting

I’ve noticed that the image quality is low when using GIF, medium to high when using JPG, and high when using PNG. File size also increases, respectively (with GIF taking the least space [usually in terms of KB] and PNG taking the most [usually in terms of MB]).

Putting It All Together

import java.awt.AWTException;
import java.awt.Dimension;
import java.awt.Rectangle;
import java.awt.Robot;
import java.awt.Toolkit;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;

import javax.imageio.ImageIO;
import javax.swing.ImageIcon;
import javax.swing.JFrame;
import javax.swing.JLabel;

public class SimpleScreenCapture {

	public static void main( String[] args ) {

		/**
		 * Set up the rectangle and set the height
		 * and width according to the user's screen
		 * size.
		 */

		Dimension dim = Toolkit.getDefaultToolkit().getScreenSize();
		Rectangle rect = new Rectangle();
		rect.width = dim.width;
		rect.height = dim.height;
		BufferedImage bi;

		/**
		 * Grab the screen and put it in a JFrame.
		 */

		try {

			JFrame frame = new JFrame();		// Frame to show the image
			Robot robot = new Robot();		// Create the robot
			bi = robot.createScreenCapture(rect);	// Take the screenshot; save in bi

			/**
			 * Here we just add the buffered image to a
			 * JLabel using ImageIcon. Then we pack the
			 * JFrame and set it to visible.
			 */

			frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
			frame.add(new JLabel(new ImageIcon(bi)));
			frame.pack();
			frame.setVisible(true);

			/**
			 * Save the file
			 */

			File f = new File("screenshot.gif");
			ImageIO.write( bi, "gif", f );

		} catch ( IOException e ) {

			e.printStackTrace();

		} catch ( AWTException e ) {

			e.printStackTrace();

		} // end try block

	} // end main method

} // end class SimpleScreenCapture

Tags: , , , , , , ,

One Response to “HOW-TO: Save a BufferedImage to Disk”

  1. Computer Cranium - Your Computer Science Resource » Blog Archive » PROJECT: Screen Capture Utility

    [...] my previous posts on how to take a screenshot and how to save an image to disk, I’ve created a Screen Capture Utility extending these basic ideas to create a program that [...]

Leave a Reply