get Clipboard Image - Java 2D Graphics

Java examples for 2D Graphics:Image

Description

get Clipboard Image

Demo Code


//package com.java2s;

import java.awt.Toolkit;
import java.awt.datatransfer.DataFlavor;
import java.awt.datatransfer.Transferable;

import java.awt.image.BufferedImage;

import javax.swing.JOptionPane;

public class Main {
    public static BufferedImage getClipboardImage() {
        Transferable t = Toolkit.getDefaultToolkit().getSystemClipboard()
                .getContents(null);/*from  ww w .j  av  a 2s. c o m*/

        try {
            if (t != null
                    && t.isDataFlavorSupported(DataFlavor.imageFlavor)) {
                BufferedImage img = (BufferedImage) t
                        .getTransferData(DataFlavor.imageFlavor);
                return img;
            }
        } catch (Exception e) {
            JOptionPane.showConfirmDialog(null, "Failed:" + e.getMessage());
        }
        return null;
    }
}

Related Tutorials