screenshot

package module
v0.0.0-...-f3698d2 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Oct 23, 2026 License: MIT Imports: 15 Imported by: 0

README

screenshot

Go Report Card

  • Go library to capture desktop screen.
  • Multiple display supported.
  • Supported GOOS: windows, darwin, linux, freebsd, openbsd, and netbsd.
  • cgo free except for GOOS=darwin.

example

  • sample program main.go

    package main
    
    import (
    	"github.com/bohdanch-w/screenshot"
    	"image/png"
    	"os"
    	"fmt"
    )
    
    func main() {
    	n := screenshot.NumActiveDisplays()
    
    	for i := 0; i < n; i++ {
    		bounds := screenshot.GetDisplayBounds(i)
    
    		img, err := screenshot.CaptureRect(bounds)
    		if err != nil {
    			panic(err)
    		}
    		fileName := fmt.Sprintf("%d_%dx%d.png", i, bounds.Dx(), bounds.Dy())
    		file, _ := os.Create(fileName)
    		defer file.Close()
    		png.Encode(file, img)
    
    		fmt.Printf("#%d : %v \"%s\"\n", i, bounds, fileName)
    	}
    }
    
  • output example

    $ go run main.go
    #0 : (0,0)-(1280,800) "0_1280x800.png"
    #1 : (-293,-1440)-(2267,0) "1_2560x1440.png"
    #2 : (-1373,-1812)-(-293,108) "2_1080x1920.png"
    $ ls -1
    0_1280x800.png
    1_2560x1440.png
    2_1080x1920.png
    main.go
    

optimization update

CaptureInto captures into a caller-provided buffer, avoiding the image allocation used by CaptureRect. On Windows/amd64, the benchmark reported:

Benchmark Time/op Memory/op Allocs/op
BenchmarkCaptureRect 68.3 ms 33,177,681 B 3
BenchmarkCapturer 66.9 ms 377,019 B 0

Measured on Windows/amd64 with a 13th Gen Intel(R) Core(TM) i5-13600KF. Results are from the supplied benchmark run and may vary by system and capture area.

coordinate

Y-axis is downward direction in this library. The origin of coordinate is upper-left corner of main display. This means coordinate system is similar to Windows OS

license

MIT Licence

author

kbinani

Documentation

Overview

Package screenshot captures screen-shot image as image.RGBA. Mac, Windows, Linux, FreeBSD, OpenBSD and NetBSD are supported.

Index

Constants

This section is empty.

Variables

View Source
var ErrUnsupported = errors.New("screenshot does not support your platform")

ErrUnsupported is returned when the platform or architecture used to compile the program does not support screenshot, e.g. if you're compiling without CGO on Darwin

Functions

func Capture

func Capture(x, y, width, height int) (*image.RGBA, error)

Capture captures the desktop region at (x, y) with the specified width and height.

func CaptureDisplay

func CaptureDisplay(displayIndex int) (*image.RGBA, error)

CaptureDisplay captures whole region of displayIndex'th display, starts at 0 for primary display.

func CaptureInto

func CaptureInto(x, y, width, height int, img *image.RGBA) error

Capture returns screen capture of specified desktop region. x and y represent distance from the upper-left corner of primary display. Y-axis is downward direction. This means coordinates system is similar to Windows OS.

func CaptureRect

func CaptureRect(rect image.Rectangle) (*image.RGBA, error)

CaptureRect captures specified region of desktop.

func GetDisplayBounds

func GetDisplayBounds(displayIndex int) (rect image.Rectangle)

GetDisplayBounds returns the bounds of displayIndex'th display. The main display is displayIndex = 0.

func NumActiveDisplays

func NumActiveDisplays() (num int)

NumActiveDisplays returns the number of active displays.

Types

type Capturer

type Capturer struct {
	// contains filtered or unexported fields
}

Capturer reuses an image buffer for successive screenshots to reduce allocations.

func NewCapturer

func NewCapturer() *Capturer

NewCapturer creates a Capturer for reusing the image buffer between captures.

func (*Capturer) Capture

func (c *Capturer) Capture() (*image.RGBA, error)

Capture captures the configured region, reusing the image buffer when possible.

func (*Capturer) Image

func (c *Capturer) Image() *image.RGBA

Image returns the most recently captured image, or nil if no capture has succeeded or the buffer was discarded after changing the capture region.

func (*Capturer) SetDisplay

func (c *Capturer) SetDisplay(index int)

SetDisplay sets the capture region to the bounds of the display at index.

func (*Capturer) SetRect

func (c *Capturer) SetRect(rect image.Rectangle)

SetRect sets the desktop region to capture and discards the previous image buffer if the region changes.

Directories

Path Synopsis
capture_display command
Capture a single display and save it as a PNG image.
Capture a single display and save it as a PNG image.
capture_into command
Capture into an image buffer allocated by the caller.
Capture into an image buffer allocated by the caller.
capturer command
Capture repeatedly with a reusable image buffer.
Capture repeatedly with a reusable image buffer.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL