SoFunction
Updated on 2025-04-13

Detailed explanation of how Electron implements screenshot function

Electron is a framework for building cross-platform desktop applications that combine with Chromium. Implementing screenshot function in Electron mainly depends ondesktopCapturerandBrowserWindowModule. Here are the steps to implement the screenshot function:

1. Install Electron

First, make sure you have Electron installed. It can be installed via npm:

npm install electron --save-dev

2. Create a basic Electron application

Create a basic Electron application structure. Here is a simple oneFile example:

const { app, BrowserWindow } = require('electron');

function createWindow() {
    const win = new BrowserWindow({
        width: 800,
        height: 600,
        webPreferences: {
            nodeIntegration: true,
        },
    });

    ('');
}

().then(createWindow);

3. Use desktopCapturer to take screenshots

In the rendering process, you can usedesktopCapturerModule to capture screens or windows. First of allAdd a button and an element to display the screenshot.

<!DOCTYPE html>
<html>
<head>
    <title>Electron Screenshot</title>
</head>
<body>
    <button id=\"capture\">Capture Screenshot</button>
    <img id=\"screenshot\" />
    <script src=\"\"></script>
</body>
</html>

Next, inAdd screenshot logic to:

const { desktopCapturer } = require('electron');

('capture').addEventListener('click', async () =&gt; {
    const sources = await ({ types: ['screen'] });

    (source =&gt; {
        if ( === 'Entire Screen') {
            // Create an image element            const img = new Image();
             = ();
            ('screenshot').src = ;
        }
    });
});

4. Process the display of screenshots

In the above code, when the "Capture Screenshot" button is clicked,Method will get the source of the screen and convert the thumbnail of the entire screen into a data URL, and then set to<img>Elementalsrcproperty. This way, the user can see screenshots of the screen.

5. Run the application

existAdd the startup script:

\"scripts\": {
    \"start\": \"electron .\"
}

Then run it on the command line:

npm start

6. Complete code example

Here is the complete code structure:

const { app, BrowserWindow } = require('electron');

function createWindow() {
    const win = new BrowserWindow({
        width: 800,
        height: 600,
        webPreferences: {
            nodeIntegration: true,
        },
    });

    ('');
}

().then(createWindow);

<!DOCTYPE html>
<html>
<head>
    <title>Electron Screenshot</title>
</head>
<body>
    <button id=\"capture\">Capture Screenshot</button>
    <img id=\"screenshot\" />
    <script src=\"\"></script>
</body>
</html>

const { desktopCapturer } = require('electron');

('capture').addEventListener('click', async () => {
    const sources = await ({ types: ['screen'] });

    (source => {
        if ( === 'Entire Screen') {
            const img = new Image();
             = ();
            ('screenshot').src = ;
        }
    });
});

Through the above steps, the screenshot function can be implemented in the Electron application.

This is the end of this article about how Electron implements screenshot function. For more related Electron screenshot content, please search for my previous articles or continue browsing the related articles below. I hope everyone will support me in the future!