xul/xpcom copy image from string to clipboard

天大地大妈咪最大 提交于 2019-12-24 00:54:56

问题


I'm stuck with no clues how to copy image to clipboard. My code looks like this:

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);

回答1:


As Neil already noted in the newsgroup, the data expected for an image is an nsIContainer instance rather than a stream. I couldn't find any examples of doing that on the web so I modified your code:

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);

For me this copies the image to clipboard correctly.



来源:https://stackoverflow.com/questions/6365550/xul-xpcom-copy-image-from-string-to-clipboard

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!