Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to capture entire WebContent inside CurrentWindow

I have a really long page inside a window (need to scroll to view all), when I try to capture the entire window using the code below, I get a squeezed image instead of full WebContent screenshot inside the CurrentWindow.

https://github.com/electron/electron/blob/master/docs/api/browser-window.md#wincapturepagerect-callback

        const remote = require('electron').remote;
        const win = remote.getCurrentWindow();
        const win_size = win.getSize();
        const win_height = win_size[0];
        const win_width = win_size[1];

        win.capturePage({
                x: 0,
                y: 0,
                width: win_width,
                height: win_height
            },
            (img) => {
                remote.require('fs')
                    .writeFile(TEMP_URL, img.toPng());
            });

I have also tried the following code but the result is the same,

        const remote = require('electron').remote;
        const webContents = remote.getCurrentWebContents();

        webContents.capturePage({
            x: 0,
            y: 0,
            width: 1000,
            height: 2000
        }, (img) => {
            remote.require('fs')
                .writeFile(TEMP_URL, img.toPng());
        });

The first object passed in into capturePage method should be the bound but turns out to be the size of the output image.

I have inspected the win_size which is the correct size of the WebContent in the CurrentWindow.

enter image description here

enter image description here

like image 433
X.Creates Avatar asked Feb 04 '17 03:02

X.Creates


1 Answers

win.getSize()

Returns an array with [width, height]. You are assigning the win_width variable to the height of the window and win_height to the width of the window. If you change these values, this may resolve your issue.

 const win_height = win_size[1];
 const win_width = win_size[0];
like image 110
Founded1898 Avatar answered Nov 15 '22 00:11

Founded1898