// 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. python标准库介绍——22 UserList 模块详解

    ==UserList 模块== ``UserList`` 模块包含了一个可继承的列表类 (事实上是对内建列表类型的 Python 封装). 在 [Example 2-16 #eg-2-16] 中, / ...

  2. 漂亮的Qt控件 QSS代码例子

    Qt Style Sheets Examples We will now see a few examples to get started with using Qt Style Sheets. S ...

  3. 大数问题:求n的阶乘

    题目:求100! 这看起来是一个非常简答的问题,递归解之毫无压力 int func(int n){ if(n <= 1) return 1; else return n*func(n-1); } ...

  4. CentOS 6.9升级GCC至7.3.0版本

    1.查看当前centos版本:  cat /etc/redhat-release 2. 安装centos6.9默认的开发工具,包含gcc,g++,make等等一系列工具: yum groupinsta ...

  5. LeetCode242——Valid Anagram

    Given two strings s and t, write a function to determine if t is an anagram of s. For example, s = & ...

  6. SourceInsight-显示文件完整路径

    使用Source insight的时候想看文件的全路径,但是默认的是中间省略的路径,所以可以通过: 1.Options-->Preferences-->Display 设置Trim lon ...

  7. MySQL PLSQL Demo - 002.变量定义、赋值

    drop procedure if exists p_hello_world; create procedure p_hello_world() begin declare v_number int; ...

  8. 将struts的jar包拷贝到WEB-INF/lib导致eclipse中配置好的javadoc失效

    我通过这个步骤导入了struts的jar包并且配置好了javadoc,且亲测可用: http://www.cnblogs.com/qrlozte/p/3173805.html 但是当我把struts的 ...

  9. js中的extend

    js中的extend   1.    JS中substring与substr的区别 之前在项目中用到substring方法,因为C#中也有字符串的截取方法Substring方法,当时也没有多想就误以为 ...

  10. 一款基于javascript的3D玻璃破碎特效

    之前为大家介绍了一款 html5 canvas实现图片玻璃碎片特效.今天要给大家带来一款基于javascript的3D玻璃破碎特效.效果图如下: 在线预览   源码下载 html代码: <div ...