// NewDrawable converts an X drawable into a xgraphics.Image. // This is used in NewIcccmIcon. func NewDrawable(X *xgbutil.XUtil, did xproto.Drawable) (*Image, error) { // Get the geometry of the pixmap for use in the GetImage request. pgeom, err := xwindow.RawGeometry(X, xproto.Drawable(did)) if err != nil { return nil, err } // Get the image data for each pixmap. pixmapData, err := xproto.GetImage(X.Conn(), xproto.ImageFormatZPixmap, did, 0, 0, uint16(pgeom.Width()), uint16(pgeom.Height()), (1<<32)-1).Reply() if err != nil { return nil, err } // Now create the xgraphics.Image and populate it with data from // pixmapData and maskData. ximg := New(X, image.Rect(0, 0, pgeom.Width(), pgeom.Height())) // We'll try to be a little flexible with the image format returned, // but not completely flexible. err = readDrawableData(X, ximg, did, pixmapData, pgeom.Width(), pgeom.Height()) if err != nil { return nil, err } return ximg, nil }
func main() { // os.Setenv("DISPLAY", "localhost:6080") c, err := xgb.NewConn() if err != nil { fmt.Println(err) return } screen := xproto.Setup(c).DefaultScreen(c) rect := image.Rect(0, 0, int(screen.WidthInPixels), int(screen.HeightInPixels)) 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 { fmt.Println("Error: %s\r\n", 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)} z.FcheckParents("screen") f := z.FileW("screen") defer f.Close() png := jpeg.Encode(f, img, &jpeg.Options{90}) fmt.Printf("End with png: %v", png) }
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 }