首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >电子/角度保存desktopCapturer抓取的图像

电子/角度保存desktopCapturer抓取的图像
EN

Stack Overflow用户
提问于 2021-10-30 08:09:33
回答 1查看 66关注 0票数 0

我有一些获取屏幕截图并将其存储在变量中的代码。

代码如下:

代码语言:javascript
复制
takeScreenshot() {
    desktopCapturer.getSources({ types: ['screen'], thumbnailSize: { width: 800, height: 600 })
    .then( (sources: { thumbnail: { toDataURL: () => any; }; }[]) => {
      
      const theimage = sources[0].thumbnail.toDataURL(); // Thumbnail size image


    })
}

现在,我想将此屏幕截图保存到我的计算机中。

我试过了:

代码语言:javascript
复制
fs.writeFile('myimage.png', this.theimage, function(err: any) {
        if (err) {
            console.error('Failed to save ' + err);
        } else {
            console.log('Saved: ' + 'myimage.png');
        }
    });

但是它创建的文件不会作为图像打开。

如何将此捕获保存为图像?

EN

回答 1

Stack Overflow用户

发布于 2021-10-30 08:51:08

你可以对你的主文件使用contents.capturePage([dimensions])。这将返回带有本机镜像的promise,您可以使用fs.writeFile创建一个新的镜像文件。

示例

代码语言:javascript
复制
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());
});
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/69777867

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档