Golang-Screenshot

// http://godoc.org/github.com/vova616/screenshot

package screenshot
import (
    "image"
    "github.com/BurntSushi/xgb"
    "github.com/BurntSushi/xgb/xproto"
)
func ScreenRect() (image.Rectangle, error) {
    c, err := xgb.NewConn()
    if err != nil {
    return image.Rectangle{}, err
    }
    defer c.Close()
    screen := xproto.Setup(c).DefaultScreen(c)
    x := screen.WidthInPixels
    y := screen.HeightInPixels
    return image.Rect(0, 0, int(x), int(y)), nil
}
func CaptureScreen() (*image.RGBA, error) {
    r, e := ScreenRect()
    if e != nil {
    return nil, e
    }
    return CaptureRect(r)
}
func CaptureRect(rect image.Rectangle) (*image.RGBA, error) {
    c, err := xgb.NewConn()
    if err != nil {
    return nil, err
    }
    defer c.Close()
    screen := xproto.Setup(c).DefaultScreen(c)
    x, y := rect.Dx(), rect.Dy()
    xImg, err := xproto.GetImage(c, xproto.ImageFormatZPixmap, xproto.Drawable(screen.Root), int16(rect.Min.X), int16(rect.Min.Y), uint16(x), uint16(y), 0xffffffff).Reply()
    if err != nil {
    return nil, err
    }
    data := xImg.Data
    for i := 0; i < len(data); i += 4 {
    data[i], data[i+2], data[i+3] = data[i+2], data[i], 255
    }
    img := &image.RGBA{data, 4 * x, image.Rect(0, 0, x, y)}
    return img, nil
}

本文来自:努力加贝

感谢作者:nulijiabei

查看原文:Golang-Screenshot

郑重声明:本站内容如果来自互联网及其他传播媒体,其版权均属原媒体及文章作者所有。转载目的在于传递更多信息及用于网络分享,并不代表本站赞同其观点和对其真实性负责,也不构成任何其他建议。