Thursday, November 4, 2010

Copy Image From Clipboard

package isuru;

 * @author Isuru
import java.awt.Toolkit;
import java.awt.datatransfer.Clipboard;
import java.awt.datatransfer.DataFlavor;
import java.awt.datatransfer.UnsupportedFlavorException;
import java.awt.image.BufferedImage;
import javax.imageio.ImageIO;

public class Main {

    public static void main(String[] args) {
        //Create clipboard object
        Clipboard clipboard = Toolkit.getDefaultToolkit().getSystemClipboard();
            //Get data from clipboard and assign it to an image
            //clipboard.getData() returns an object, so we need to cast it to a BufferedImage
            BufferedImage image = (BufferedImage)clipboard.getData(DataFlavor.imageFlavor);

            //file that we'll save to disk
            File file = new File("image.jpg");

             * class to write image to disk. You specify the image
             * to be saved, its type, and then the file in which to write the image data.

            ImageIO.write(image, "jpg", file);

        }catch(UnsupportedFlavorException ufe){
        catch(IOException ioe){

Found from a online forum and tested in my system.  

No comments:

Post a Comment