I'm trying to capture a window and save it as JPEG or PNG file, however, capture function does now work Here the code:
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)
})
And this is the error in the terminal:
(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.
I don't know Electron, but just looking at the APIs and the error message, you are missing a quality parameter for toJPEG()
(See https://www.electronjs.org/docs/api/native-image#imagetojpegquality ). Supply a number between 1 and 100 when calling that function and I imagine you'll be good.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.