How can I take a screenshot of a window in nodejs?

I have research to find a way to take a screenshot using nodejs, and I'm trying to do it using node -ffi, but I don’t know how ... while 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);

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,

Can anybody help me?

+4
source share
1 answer

You can use the NPM package called desktop-screenshot . "It is very easy to use.

NPM example:

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

+3
source

Source: https://habr.com/ru/post/1673256/


All Articles