CaptureScreen give me always the same ghost image
Opened this issue · 2 comments
Hello
I'm at one problem to use your package for my dev.
The problem is that when i try to CaptureScreenshot the foregroung window
Either i get a dark image (when vscode is focused), either i get the same image from my fullscreen game (i can teleport to another place in my game, the screenshot will still be as the first one)
Its seems there is a problem somewhere but i don't know what could cause this
I also check with window detective, nothing is blocking the screenshot for both the game and vscode app
Here is the code used to reproduce the bug
import * as fs from 'fs';
import { Screenshot, VRect } from 'windows-ffi';
import { CaptureScreenshot, GetForegroundWindowHandle } from 'windows-ffi';
import sharp from 'sharp';
let start: number = Date.now();
// First capture a screenshot of a section of the screen.
const screenshot: Screenshot = CaptureScreenshot({
windowHandle: GetForegroundWindowHandle(), // comment to screenshot all windows
rectToCapture: new VRect(0, 0, 2560, 1440),
log: true,
});
console.log(Date.now() - start);
start = Date.now();
const buffer: sharp.Sharp = await sharp(Buffer.from(screenshot.buffer), {
raw: {
width: 2560,
height: 1440,
channels: 4,
},
});
fs.writeFileSync('./debug/ffi.jpeg', await buffer.jpeg().toBuffer());
console.log(Date.now() - start);
perhaps i need another/older user32 dll ?
Node version : v19.7.0
Windows 11
Thanks in advance for your help
@xDelph can you try removing the windowHandle and check if it works for you?
I was able to reproduce the same behavior but only when specifying a windowHandle.
@Venryx any idea why this is happening? It seems there's some kind of cache, i tried using the Release functions but i don't have much experience with this APIs any help would be appreciated
From kaue:
@xDelph can you try removing the windowHandle and check if it works for you?
I was able to reproduce the same behavior but only when specifying a windowHandle.
@Venryx any idea why this is happening? It seems there's some kind of cache, i tried using the Release functions but i don't have much experience with this APIs any help would be appreciated
Sorry, but I don't know either. My best guess is that when a window handle is supplied (for certain programs/windows), what gets returned is some sort of buffer stored for that window instead of the latest screen data for that region (perhaps the buffer that is displayed in windows taskbar when hovering?). I have not encountered the issue myself (I only use this library for a couple of things), but that's my speculation.
@xDelph Like kaue suggested, try taking a screenshot with the windowHandle: GetForegroundWindowHandle(),
line commented out. If that screenshot has correct contents, then what you can do is use a call to user32.GetWindowRect
(as mentioned here and here, with possibly working code example here) to get the window's rectangle, and then supply that in the rectToCapture
property sent to CaptureScreenshot
.