// If an image is on the system clipboard, this method returns it;
// otherwise it returns null.
public static Image getClipboard() {
Transferable t = Toolkit.getDefaultToolkit().getSystemClipboard().getContents(null); try {
if (t != null && t.isDataFlavorSupported(DataFlavor.imageFlavor)) {
Image text = (Image)t.getTransferData(DataFlavor.imageFlavor);
return text;
}
} catch (UnsupportedFlavorException e) {
} catch (IOException e) {
}
return null;
}

Setting an image on the system clipboard requires a custom Transferable object to hold the image while on the clipboard.

    // This method writes a image to the system clipboard.
// otherwise it returns null.
public static void setClipboard(Image image) {
ImageSelection imgSel = new ImageSelection(image);
Toolkit.getDefaultToolkit().getSystemClipboard().setContents(imgSel, null);
} // This class is used to hold an image while on the clipboard.
public static class ImageSelection implements Transferable {
private Image image; public ImageSelection(Image image) {
this.image = image;
} // Returns supported flavors
public DataFlavor[] getTransferDataFlavors() {
return new DataFlavor[]{DataFlavor.imageFlavor};
} // Returns true if flavor is supported
public boolean isDataFlavorSupported(DataFlavor flavor) {
return DataFlavor.imageFlavor.equals(flavor);
} // Returns image
public Object getTransferData(DataFlavor flavor) throws UnsupportedFlavorException, IOException {
if (!DataFlavor.imageFlavor.equals(flavor)) {
throw new UnsupportedFlavorException(flavor);
}
return image;
}
}
Related Examples

e638. 向剪切板获取和粘贴图像的更多相关文章

  1. e637. 向剪切板获取和粘贴文本

    This examples defines methods for getting and setting text on the system clipboard. // If a string i ...

  2. 重新想象 Windows 8 Store Apps (40) - 剪切板: 复制/粘贴文本, html, 图片, 文件

    [源码下载] 重新想象 Windows 8 Store Apps (40) - 剪切板: 复制/粘贴文本, html, 图片, 文件 作者:webabcd 介绍重新想象 Windows 8 Store ...

  3. 背水一战 Windows 10 (102) - 应用间通信: 剪切板

    [源码下载] 背水一战 Windows 10 (102) - 应用间通信: 剪切板 作者:webabcd 介绍背水一战 Windows 10 之 应用间通信 剪切板 - 基础, 复制/粘贴 text ...

  4. js实现复制内容到剪切板,兼容pc和手机端,支持Safari浏览器

    Javascript原生有一些事件:copy.paste.cut, 这些事件可以作用的目标元素: 能获得焦点的元素 (如contentEditable内容能编辑或者可以选中的元素),或者是<bo ...

  5. C#操作剪切板(Clipboard)

    剪切板是Windows系统提供的功能,从我最早接触到的Windows 3.2版本开始,就一直带着了.以前使用C++的时候,是直接使用Windows API对其进行操作的,到了.NET下,在WinFor ...

  6. [转载]3.5 UiPath对剪切板的介绍和使用

    一.剪切板操作的介绍 模拟用户使用剪切板操作的一种行为: 例如使用设置剪切板(SettoClipboard),从剪切板获取(GetfromClipboard)的操作 二.剪切板相关操作在UiPath中 ...

  7. Unity3d 复制文字到剪切板及存储图像到相册

    游戏中里开发分享功能时用到两个小功能:1.复制一个链接到剪切板供在其他应用粘贴分享使用,2.保存一张二维码图像到相册供发送给其他应用用于分享.但是在unity中无法完成,需要分别开发相应的插件. An ...

  8. 浏览器中用JavaScript获取剪切板中的文件

    本文转自我的个人网站  , 原文地址:http://www.zoucz.com/blog/2016/01/29/get-file-from-clipboard/  ,欢迎前往交流讨论 在网页上编辑内容 ...

  9. C++打开剪切板,获取剪切板数据

    if (::OpenClipboard(NULL) && ::IsClipboardFormatAvailable(CF_HDROP)) { HDROP hDrop = (HDROP) ...

随机推荐

  1. STM32 mdk软件仿真时过不去时钟的问题

    stm32的程序用MDK软件仿真时,由于系统时钟初始化函数里有个等待系统时钟准备好的循环,所以过不去. 设置方式如下:这么设置之后仿真时就可以直接进入main函数了.

  2. 【iOS】UIWebView HTML5 扩展

    对于不少iOS开发人员来说,HTML5的内容比較陌生. 尤其是UIWebView类的 stringByEvaluatingJavaScriptFromString 方法 让非常多人认为又得学一种新的语 ...

  3. 使用C语言调用mysql数据库编程实战以及技巧

    今天编写使用C语言调用mysql数据库编程实战以及技巧.为其它IT同行作为參考,当然有错误能够留言,共同学习. 一.mysql数据库的C语言经常使用接口API 1.首先当然是链接数据库mysql_re ...

  4. [svc]linux日志和安全日志

    last详解: http://www.cnblogs.com/kerrycode/p/4223751.html

  5. ClickAndMoveTest

    关于ccTouchesEnded看这个博客即可 http://blog.linguofeng.com/archive/2012/09/12/cocos2d-x-touch.html class Cli ...

  6. JVM ,Java paper

    http://files.cnblogs.com/files/WCFGROUP/IntroductiontoCompilerConstructioninaJavaWorld.pdf A Fast Wr ...

  7. angular -- $route API翻译

    $route -$routeProvider服务 -依赖ngRoute模块 $route能够在路径发生改变的时候,渲染不同的视图,调用不同的控制器.它监测了$location.url(),然后根据路径 ...

  8. Spring Boot干货系列:(五)开发Web应用JSP篇

    Spring Boot干货系列:(五)开发Web应用JSP篇 原创 2017-04-05 嘟嘟MD 嘟爷java超神学堂 前言 上一篇介绍了Spring Boot中使用Thymeleaf模板引擎,今天 ...

  9. JS 计算1到1000000个自然数中有几个1的自然数?

    <script> window.onload=function(){ var   arr=[]; for(var i=1;i<1000001;i++) { var stri= i.t ...

  10. System.Data.SqlClient.SqlError:无法打开备份设备'D:\..\abc.bak'

    在SQL Server中备份数据库时遇到备份对于服务器“服务器名”失败. (Microsoft.SqlServer.Smo)其他信息:System.Data.SqlClient.SqlError:无法 ...