若操作系统的剪贴板中存在图像数据,则可将其保持为指定格式的文件,如PNG或BITMAP等格式,以保存BITMAP格式为例
import org.eclipse.swt.dnd.Clipboard;
import org.eclipse.swt.dnd.ImageTransfer;
import org.eclipse.swt.graphics.ImageData;
import org.eclipse.swt.graphics.ImageLoader;
Clipboard clipboard = new Clipboard(display);
ImageData imageData = (ImageData)clipboard.getContents(ImageTransfer.getInstance());
if (imageData != null) {
ImageLoader loader = new ImageLoader();
loader.data = new ImageData[] {imageData};
Display.getCurrent().asyncExec(new Runnable() {
public void run() {
loader.save(path + "//" + fileName + ".BMP", SWT.BITMAP);
}
});
}