2011-06-15 104 views
2

我無法找到線索如何將圖像複製到剪貼板。 我的代碼如下所示:xul/xpcom將圖像從字符串複製到剪貼板

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

你從哪裏得到(base64編碼)的圖像數據? – Neil 2011-06-19 23:46:10

+0

canvas.toDataURL – nweb 2011-06-23 11:00:05

回答

2

尼爾在新聞組已經指出,預期的圖像數據是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); 

對我來說這將映像複製到剪貼板正確。

+0

由於某些原因剪貼板在macos上保持空白(ff5) – nweb 2011-08-06 10:57:57