gpt4 book ai didi

javascript - xul/xpcom 将图像从字符串复制到剪贴板

转载 作者:行者123 更新时间:2023-11-29 22:32:46 28 4
gpt4 key购买 nike

我不知道如何将图像复制到剪贴板。我的代码如下所示:

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

最佳答案

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

对我来说,这会将图像正确地复制到剪贴板。

关于javascript - xul/xpcom 将图像从字符串复制到剪贴板,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6365550/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com