我没有线索如何将图像复制到剪贴板。 我的代码如下所示:
var image = "data:image/png;base64,..."
var io = Components.classes["@mozilla.org/network/io-service;1"].getService(Components.interfaces.nsIIOService);
var channel = io.newChannel(image, null, null);
var input = channel.open();
var trans = Components.classes["@mozilla.org/widget/transferable;1"].createInstance(Components.interfaces.nsITransferable);
trans.addDataFlavor("image/png");
trans.setTransferData("image/png", input, input.available());
var clipid = Components.interfaces.nsIClipboard;
var clip = Components.classes["@mozilla.org/widget/clipboard;1"].getService(clipid);
clip.setData(trans, null, clipid.kGlobalClipboard);
答案 0 :(得分:2)
正如Neil在新闻组中已经注意到的那样,图像的预期数据是nsIContainer
实例,而不是流。我在网上找不到任何这样做的例子,所以我修改了你的代码:
var image = "data:image/png;base64,...";
var io = Components.classes["@mozilla.org/network/io-service;1"].getService(Components.interfaces.nsIIOService);
var channel = io.newChannel(image, null, null);
var input = channel.open();
var imgTools = Components.classes["@mozilla.org/image/tools;1"].getService(Components.interfaces.imgITools);
var container = {};
imgTools.decodeImageData(input, channel.contentType, container);
var trans = Components.classes["@mozilla.org/widget/transferable;1"].createInstance(Components.interfaces.nsITransferable);
trans.addDataFlavor("image/png");
trans.setTransferData("image/png", container.value, -1);
var clipid = Components.interfaces.nsIClipboard;
var clip = Components.classes["@mozilla.org/widget/clipboard;1"].getService(clipid);
clip.setData(trans, null, clipid.kGlobalClipboard);
对我来说,这会正确地将图像复制到剪贴板。