Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I take a window screenshot in Node.js?

I'm in a research to find a way to take a screenshot of a window using Node.js, and I'm trying to do this with node-ffi, but I don't know how... at a time I'm stuck here:

var ffi = require('ffi');

var user32 = new ffi.Library("user32", {
      FindWindowA: [ 'uint32' , [ 'string', 'string' ]]
    , PrintWindow: [ 'int32'  , [ 'int32', 'string', 'int32' ]]
});

var IMG;
var windowHandle = user32.FindWindowA(null, "Calculator");
var printWin = user32.PrintWindow(windowHandle, IMG, 0);

console.log(printWin);
console.log(IMG);

The result:

$ node get-print.js
1
undefined

EDITED

I found the following working code in C++

Bitmap bm = new Bitmap(1024, 768);
Graphics g = Graphics.FromImage(bm);
IntPtr hdc = g.GetHdc();
Form1.PrintWindow(this.Handle, hdc, 0);
g.ReleaseHdc(hdc);
g.Flush();
g.Dispose();
this.pictureBox1.Image = bm;

now I need to do this on NodeJs,

Anyone can help me?

like image 832
Daniel de Andrade Varela Avatar asked Mar 27 '17 00:03

Daniel de Andrade Varela


People also ask

How do you take a screenshot in node JS?

goto("https://nytimes.com"); await page. screenshot({ path: "nyt-puppeteer. png" }); await browser. close(); });

How do I capture an entire window?

Press Ctrl + PrtScn keys. The entire screen changes to gray including the open menu. Select Mode, or in earlier versions of Windows, select the arrow next to the New button. Select the kind of snip you want, and then select the area of the screen capture that you want to capture.

How do I take a screenshot using JavaScript?

1) Using the html2canvas JavaScript library It sets the target to append the output screenshot to the HTML body. Generates canvas element and appends to the HTML. It gets the image source data URL from the canvas object. Push the source URL to the PHP via AJAX to save the screenshot to the server.


2 Answers

There's also an alternative Node.js package that's still being worked on at the moment (last commit from 15 days ago; compare to the package mentioned above, which has last commit from 2015 or 2016). It allows to choose the screen it captures, which the other one doesn't seem to do.

https://github.com/bencevans/screenshot-desktop

const screenshot = require('screenshot-desktop');

screenshot.listDisplays().then((displays) => {
  // displays: [{ id, name }, { id, name }]
  screenshot({ screen: displays[displays.length - 1].id })
    .then((img) => {
      // img: Buffer of screenshot of the last display
    });
})
like image 83
okram Avatar answered Oct 17 '22 00:10

okram


You could use a NPM package called "desktop-screenshot". It's very simple to use.

Example on NPM:

var screenshot = require('desktop-screenshot');

screenshot("screenshot.png", function(error, complete) {
    if(error)
        console.log("Screenshot failed", error);
    else
        console.log("Screenshot succeeded");
});

https://www.npmjs.com/package/desktop-screenshot

like image 29
Matheus Salmi Avatar answered Oct 16 '22 23:10

Matheus Salmi