Javascript 如何从电子窗口捕获图像?

Javascript 如何从电子窗口捕获图像?,javascript,node.js,electron,Javascript,Node.js,Electron,我正在尝试捕获一个窗口并将其保存为JPEG或PNG文件,但是,捕获功能现在可以工作了 代码如下: const { app, BrowserWindow } = require('electron') const fs = require("fs"); const path = require("path") app.on("ready", () => { //This is the window I want to capt

我正在尝试捕获一个窗口并将其保存为JPEG或PNG文件,但是,捕获功能现在可以工作了 代码如下:

const { app, BrowserWindow } = require('electron')
const fs = require("fs");
const path = require("path")

app.on("ready", () => {
  //This is the window I want to capture
  let win = new BrowserWindow({
    width: 900, height: 680, webPreferences: {
      nodeIntegration: true
    }
  });
  win.loadFile(path.join(__dirname, 'index.html'))

  //Timeout incase the window didn't completely load
  setTimeout(async () => {
    //NativeImage to be captured
    let img2 = await (await win.capturePage()).toJPEG();

    //Save to file
    fs.writeFile('newfile.jpeg', img2, function (err) {
      if (err) throw err;
      console.log('File is created successfully.');
    });
    fs.close();
  }, 1000)
})
这是终端中的错误:

(electron) The default value of app.allowRendererProcessReuse is deprecated, it is currently "false".  It will change to be "true" in Electron 9.  For more information please check https://github.com/electron/electron/issues/18397
(node:19684) UnhandledPromiseRejectionWarning: TypeError: Insufficient number of arguments.
    at Timeout._onTimeout (D:\Cluttered Projects\DesktopApps\electronTest\src\index.js:19:47)
(node:19684) UnhandledPromiseRejectionWarning: TypeError: Insufficient number of arguments.
    at Timeout._onTimeout (D:\Cluttered Projects\DesktopApps\electronTest\src\index.js:19:47)
(node:19684) UnhandledPromiseRejectionWarning: Unhandled promise rejection. This error originated either by throwing inside of an async function without a catch block, or by rejecting a promise which was not handled with .catch(). (rejection id: 1)
(node:19684) UnhandledPromiseRejectionWarning: Unhandled promise rejection. This error originated either by throwing inside of an async function without a catch block, or by rejecting a promise which was not handled with .catch(). (rejection id: 1)
(node:19684) [DEP0018] DeprecationWarning: Unhandled promise rejections are deprecated. In the future, promise rejections that are not handled will terminate the Node.js process with a non-zero exit code.
(node:19684) [DEP0018] DeprecationWarning: Unhandled promise rejections are deprecated. In the future, promise rejections that are not handled will terminate the Node.js process with a non-zero exit code.

我不知道Electron,但只要看看API和错误消息,您就缺少了
toJPEG()
(请参阅)的质量参数。调用该函数时,请提供一个介于1和100之间的数字,我想你会很好。

如果没有传递参数,该例程没有默认值,这让我感到惊讶。非常感谢