How Can I Take A Window Screenshot In Node.js?
Solution 1:
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
Solution 2:
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>usingnamespace v8;
voidGetImage(const FunctionCallbackInfi<Value>& args){
Bitmap bm = newBitmap(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);
}
voidInitialize(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 toreturn a base64 string instead, then insert: ,"base64"*/);
Solution 3:
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
});
})
Solution 4:
If you require pixel-by-pixel access to the screenshot data from NodeJS (and don't want to have to write then read it from disk), you can use the CaptureScreenshot
function of my windows-ffi package.
Usage:
import {VRect, CaptureScreenshot, GetForegroundWindowHandle} from"windows-ffi";
// First capture a screenshot of a section of the screen.const screenshot = CaptureScreenshot({
windowHandle: GetForegroundWindowHandle(), // comment to screenshot all windowsrectToCapture: newVRect(0, 0, 800, 600),
});
// The image-data is now stored in the `screenshot.buffer` Buffer object.// Access it directly (and cheaply) using the helper functions on `screenshot`.for (let x = 0; x < 800; x++) {
console.log(`Pixel color at [${x}, 0] is:`, screenshot.GetPixel(x, 0).ToHex_RGB());
}
Naturally, this only works on Windows, since it uses the Windows API, through ffi-napi.
Post a Comment for "How Can I Take A Window Screenshot In Node.js?"