screen.go 1.5 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162
  1. // Copyright 2016 The go-vgo Project Developers. See the COPYRIGHT
  2. // file at the top-level directory of this distribution and at
  3. // https://github.com/go-vgo/robotgo/blob/master/LICENSE
  4. //
  5. // Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
  6. // http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
  7. // <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
  8. // option. This file may not be copied, modified, or distributed
  9. // except according to those terms.
  10. package robotgo
  11. import (
  12. "image"
  13. "github.com/kbinani/screenshot"
  14. )
  15. // GetDisplayBounds gets the display screen bounds
  16. func GetDisplayBounds(i int) (x, y, w, h int) {
  17. bs := screenshot.GetDisplayBounds(i)
  18. return bs.Min.X, bs.Min.Y, bs.Dx(), bs.Dy()
  19. }
  20. // GetDisplayRect gets the display rect
  21. func GetDisplayRect(i int) Rect {
  22. x, y, w, h := GetDisplayBounds(i)
  23. return Rect{
  24. Point{X: x, Y: y},
  25. Size{W: w, H: h}}
  26. }
  27. // Capture capture the screenshot, use the CaptureImg default
  28. func Capture(args ...int) (*image.RGBA, error) {
  29. displayId := 0
  30. if DisplayID != -1 {
  31. displayId = DisplayID
  32. }
  33. if len(args) > 4 {
  34. displayId = args[4]
  35. }
  36. var x, y, w, h int
  37. if len(args) > 3 {
  38. x, y, w, h = args[0], args[1], args[2], args[3]
  39. } else {
  40. x, y, w, h = GetDisplayBounds(displayId)
  41. }
  42. return screenshot.Capture(x, y, w, h)
  43. }
  44. // SaveCapture capture screen and save the screenshot to image
  45. func SaveCapture(path string, args ...int) error {
  46. img, err := CaptureImg(args...)
  47. if err != nil {
  48. return err
  49. }
  50. return Save(img, path)
  51. }