我正在 try catch 一个窗口并将其另存为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()
的质量参数(请参阅https://www.electronjs.org/docs/api/native-image#imagetojpegquality)。调用该函数时,请提供一个介于1到100之间的数字,我想您会很好。
关于javascript - 如何从 Electron 窗口中捕获图像?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/62565587/