screenshot-desktop

repository·main·Indexed 19 days ago

https://github.com/bencevans/screenshot-desktop

A multi-platform, Promise-based library for capturing screenshots of local machines. It supports capturing specific displays or all screens at once, with outputs available as Buffers or saved to disk in JPG and PNG formats. Version 1.15.4.

Tokens
930
Snippets
5
Records
5
Agent score
18%

What's inside screenshot-desktop

  1. Platform-specific requirements for Linux

    main

    While OSX and Windows have no dependencies, Linux users must have an image library installed. By default, the library expects ImageMagick. You can configure which library to use via the linuxLibrary option.

    # Required for Linux
    apt-get install imagemagick
  2. Capture a screenshot using screenshot()

    main

    The primary API is a Promise-based screenshot() function. By default, it returns a Buffer containing the screenshot in JPG format. You can specify a different format or a filename to save the output directly to disk.

    const screenshot = require('screenshot-desktop')
    
    // Returns a Buffer of the default JPG screenshot
    screenshot().then((img) => {
      // img: Buffer filled with jpg goodness
    }).catch((err) => {
      // ...
    })
    
    // Returns a Buffer of a PNG screenshot
    screenshot({format: 'png'}).then((img) => {
      // img: Buffer filled with png goodness
    })
    
    // Saves the screenshot to a file and returns the absolute path
    screenshot({ filename: 'shot.jpg' }).then((imgPath) => {
      // imgPath: absolute path to screenshot
    })
  3. Capture specific displays or all screens

    main

    You can use screenshot.listDisplays() to retrieve an array of available displays, each containing an id and name. You can then pass a specific screen ID to the screenshot() options to capture only that display. Alternatively, use screenshot.all() to capture all screens at once, which returns an array of Buffers.

    const screenshot = require('screenshot-desktop')
    
    // Capture a specific display by ID
    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
        });
    })
    
    // Capture all screens at once
    screenshot.all().then((imgs) => {
      // imgs: an array of Buffers, one for each screen
    })
  4. Configure screenshot() options

    main

    The screenshot() function accepts an optional configuration object to control output and behavior.

    // Options Reference:
    // - filename: Optional. Absolute or relative path to save output.
    // - format: Optional. Valid values: 'png' | 'jpg'.
    // - linuxLibrary: Optional. Linux only. Valid values: 'scrot' | 'imagemagick'.
    //   Note: 'scrot' does not support 'format' or 'screen' selection.