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

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
    });
})

Although I don't have full code working, but in theory if you're able to do so in C++, then simply use node-gyp to compile the C++ file to a .node file, then include that in you're nodeJS file.

So some example pseudo-code, first of all make a binding.gyp file in a new directory, and put some code in it like this:

{
  "targets": [
    {
        "target_name": "addon",
        "sources": [ 
            "hi.cc"
        ]
    }
  ]
}

then in that same directory (for now) make another file called hi.cc, and put your C++ code in it, plus some more to make a node module out of it. So, based on the docs mentioned above, you could do something like this (untested):

/*don't know what includes you're using to git the Bitmap 
and  Graphics functions, but include them here */

 /*then to make a node module*/
#include <node.h>

using namespace v8;


void GetImage(const FunctionCallbackInfi<Value>& args) {
    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();
    /*
    this is the key part, although I'm not
    100% sure it will work since I don't 
    know exactly what type Graphics returns, 
    but basically just convert it somehow into 
    base64, or a plain old void* value
    (as in this following example), then make a new
    Local variable of it and set the return type
    (or make a function callback). So first get the 
    Graphics variable into a void* of the data, then 
    convert it to an ArrayBuffer to use in NodeJS, based on this
    answer. Anyway:
    */
    Local<
        ArrayBuffer
    > 
    v = 
    ArrayBuffer::New(i, /*some void* value*/ temp, 5000/*or some length*/);
    a.GetReturnValue().Set(v);
}


void Initialize(Local<Object> exports) {
  NODE_SET_METHOD(exports, "hello", GetImage);
}

NODE_MODULE(NODE_GYP_MODULE_NAME, Initialize)

Then make sure you actually have node-gyp and the proper build tools installed (see docs above, but its pretty much npm i -g node-gyp), then go to build -> Release -> addon.node and copy it to your main nodeJS directory, then make a new nodeJS file or include the following in an existing one:

let addon = require("./addon"),
    pictureData = Buffer.from(addon.hello()/* if you choose to return a base64 string instead, then insert: ,"base64"*/);

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