Debian Codegraph @

example/main.go

raw
1 package main
2
3 import (
4 "fmt"
5 "image"
6 "image/png"
7 "os"
8
9 "github.com/kbinani/screenshot"
10 )
11
12 // save *image.RGBA to filePath with PNG format.
13 func save(img *image.RGBA, filePath string) {
14 file, err := os.Create(filePath)
15 if err != nil {
16 panic(err)
17 }
18 defer file.Close()
19 err = png.Encode(file, img)
20 if err != nil {
21 panic(err)
22 }
23 }
24
25 func main() {
26 // Capture each displays.
27 n := screenshot.NumActiveDisplays()
28 if n <= 0 {
29 panic("Active display not found")
30 }
31
32 var all image.Rectangle = image.Rect(0, 0, 0, 0)
33
34 for i := 0; i < n; i++ {
35 bounds := screenshot.GetDisplayBounds(i)
36 all = bounds.Union(all)
37
38 img, err := screenshot.CaptureRect(bounds)
39 if err != nil {
40 panic(err)
41 }
42 fileName := fmt.Sprintf("%d_%dx%d.png", i, bounds.Dx(), bounds.Dy())
43 save(img, fileName)
44
45 fmt.Printf("#%d : %v \"%s\"\n", i, bounds, fileName)
46 }
47
48 // Capture all desktop region into an image.
49 fmt.Printf("%v\n", all)
50 img, err := screenshot.Capture(all.Min.X, all.Min.Y, all.Dx(), all.Dy())
51 if err != nil {
52 panic(err)
53 }
54 save(img, "all.png")
55 }