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?
goto("https://nytimes.com"); await page. screenshot({ path: "nyt-puppeteer. png" }); await browser. close(); });
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.
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.
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
});
})
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
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With