我有一些获取屏幕截图并将其存储在变量中的代码。
代码如下:
takeScreenshot() {
desktopCapturer.getSources({ types: ['screen'], thumbnailSize: { width: 800, height: 600 })
.then( (sources: { thumbnail: { toDataURL: () => any; }; }[]) => {
const theimage = sources[0].thumbnail.toDataURL(); // Thumbnail size image
})
}现在,我想将此屏幕截图保存到我的计算机中。
我试过了:
fs.writeFile('myimage.png', this.theimage, function(err: any) {
if (err) {
console.error('Failed to save ' + err);
} else {
console.log('Saved: ' + 'myimage.png');
}
});但是它创建的文件不会作为图像打开。
如何将此捕获保存为图像?
发布于 2021-10-30 08:51:08
你可以对你的主文件使用contents.capturePage([dimensions])。这将返回带有本机镜像的promise,您可以使用fs.writeFile创建一个新的镜像文件。
示例
const {
app,
BrowserWindow
} = require('electron');
const fs = require('fs');
const initWindow = () => {
const window = new BrowserWindow({
width: 800,
height: 600,
});
window.loadFile('index.html');
return window;
}
app.once('ready', async () => {
const window = initWindow();
const image = await window.webContents.capturePage();
fs.writeFile('image.png', image.toPNG());
});https://stackoverflow.com/questions/69777867
复制相似问题