简体   繁体   English

如何从 Electron window 捕获图像?

[英]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.

相关问题 如何在 Electron 中将窗口图标设置为缓冲区图像? - How do I set a window icon to a Buffer image in Electron? 如何从电子的特定窗口中删除菜单栏? - How do I remove the menu bar from a specific window in electron? 我如何确保只能从电子窗口内部访问Electron Express应用 - How do i make sure that Electron express app is only accessible from inside electron window 电子:如何与浏览器窗口通信? - Electron: How do I communicate with browser window? 如何启用在 Electron 和 Svelte 中移动无框架 window 的功能? - How do I enable the ability to move a frameless window in Electron and Svelte? 如何在不“切换”电子应用程序的情况下将窗口置于最前面? - How do I bring a window to the front, without "switching" apps in electron? 如何为 electron 中的特定 window 制作单独的菜单? - How do i make a separate menu for a specific window in electron? 如何多次调用 Electron 浏览器 Window? - How do i call an Electron Browser Window multiple times? 如何在电子js中创建新窗口,以使原始窗口在关闭新窗口之前不可用? - How do I create a new Window in electron js such that the original window is unusable until i close the new window? 我如何打开Chrome并通过节点/电子应用程序执行Google图片搜索 - How can I open chrome and do a google image search from a node/electron application
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM