aminal/gui/vendor/github.com/kbinani/screenshot/example/main.go

53 lines
1017 B
Go

package main
import (
"fmt"
"image"
"image/png"
"os"
"github.com/kbinani/screenshot"
)
// save *image.RGBA to filePath with PNG format.
func save(img *image.RGBA, filePath string) {
file, err := os.Create(filePath)
if err != nil {
panic(err)
}
defer file.Close()
png.Encode(file, img)
}
func main() {
// Capture each displays.
n := screenshot.NumActiveDisplays()
if n <= 0 {
panic("Active display not found")
}
var all image.Rectangle = image.Rect(0, 0, 0, 0)
for i := 0; i < n; i++ {
bounds := screenshot.GetDisplayBounds(i)
all = bounds.Union(all)
img, err := screenshot.CaptureRect(bounds)
if err != nil {
panic(err)
}
fileName := fmt.Sprintf("%d_%dx%d.png", i, bounds.Dx(), bounds.Dy())
save(img, fileName)
fmt.Printf("#%d : %v \"%s\"\n", i, bounds, fileName)
}
// Capture all desktop region into an image.
fmt.Printf("%v\n", all)
img, err := screenshot.Capture(all.Min.X, all.Min.Y, all.Dx(), all.Dy())
if err != nil {
panic(err)
}
save(img, "all.png")
}