Search...
FAQs
Subscribe
Pie
FAQs
Recent topics
Flagged topics
Hot topics
Best topics
Search...
Search within Swing / AWT / SWT
Search Coderanch
Advance search
Google search
Register / Login
Post Reply
Bookmark Topic
Watch Topic
New Topic
programming forums
Java
Mobile
Certification
Databases
Caching
Books
Engineering
Micro Controllers
OS
Languages
Paradigms
IDEs
Build Tools
Frameworks
Application Servers
Open Source
This Site
Careers
Other
Pie Elite
all forums
this forum made possible by our volunteer staff, including ...
Marshals:
Campbell Ritchie
Jeanne Boyarsky
Ron McLeod
Paul Clapham
Liutauras Vilda
Sheriffs:
paul wheaton
Rob Spoor
Devaka Cooray
Saloon Keepers:
Stephan van Hulst
Tim Holloway
Carey Brown
Frits Walraven
Tim Moores
Bartenders:
Mikalai Zaikin
Forum:
Swing / AWT / SWT
BufferedImage to System Clipboard
Gregg Bolinger
Ranch Hand
Posts: 15304
6
I like...
posted 21 years ago
Number of slices to send:
Optional 'thank-you' note:
Send
Does anyone know how to transfer a BufferedImage to the System Clipboard? Or if it is even possible?
Nathan Pruett
Bartender
Posts: 4121
I like...
posted 21 years ago
Number of slices to send:
Optional 'thank-you' note:
Send
Yep... it's possible...
import java.awt.*; import java.awt.datatransfer.*; import java.awt.image.*; import java.io.*; public class ClipboardImage implements ClipboardOwner { public ClipboardImage() { try { Robot robot = new Robot(); Dimension screenSize = Toolkit.getDefaultToolkit().getScreenSize(); Rectangle screen = new Rectangle( screenSize ); BufferedImage i = robot.createScreenCapture( screen ); TransferableImage trans = new TransferableImage( i ); Clipboard c = Toolkit.getDefaultToolkit().getSystemClipboard(); c.setContents( trans, this ); } catch ( AWTException x ) { x.printStackTrace(); System.exit( 1 ); } } public static void main( String[] arg ) { ClipboardImage ci = new ClipboardImage(); } public void lostOwnership( Clipboard clip, Transferable trans ) { System.out.println( "Lost Clipboard Ownership" ); } private class TransferableImage implements Transferable { Image i; public TransferableImage( Image i ) { this.i = i; } public Object getTransferData( DataFlavor flavor ) throws UnsupportedFlavorException, IOException { if ( flavor.equals( DataFlavor.imageFlavor ) && i != null ) { return i; } else { throw new UnsupportedFlavorException( flavor ); } } public DataFlavor[] getTransferDataFlavors() { DataFlavor[] flavors = new DataFlavor[ 1 ]; flavors[ 0 ] = DataFlavor.imageFlavor; return flavors; } public boolean isDataFlavorSupported( DataFlavor flavor ) { DataFlavor[] flavors = getTransferDataFlavors(); for ( int i = 0; i < flavors.length; i++ ) { if ( flavor.equals( flavors[ i ] ) ) { return true; } } return false; } } }
-Nate
-Nate
Write once, run anywhere, because there's nowhere to hide! - /. A.C.
Gregg Bolinger
Ranch Hand
Posts: 15304
6
I like...
posted 21 years ago
Number of slices to send:
Optional 'thank-you' note:
Send
Nate, you are the man. As usual!!
reply
reply
Bookmark Topic
Watch Topic
New Topic
Boost this thread!
Similar Threads
How to get files copied to office clipboard?
Capturing an image to system clipboard
copying JFrame into Clipboard - urgent
How to Export to Ms-Word?
image to clipboard
More...