[英]How do I capture an image from a Electron window?
I'm trying to capture a window and save it as JPEG or PNG file, however, capture function does now work Here the code:我正在尝试捕获 window 并将其保存为 JPEG 或 PNG 文件,但是,捕获 function 现在可以正常工作这里的代码:
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 ).我不知道 Electron,但仅查看 API 和错误消息,您缺少
toJPEG()
的质量参数(请参阅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.调用 function 时提供 1 到 100 之间的数字,我想你会很好。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.