screenshot

package module
v0.0.0-...-47b0c5d Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Jun 16, 2023 License: MIT Imports: 8 Imported by: 0

README

screenshot

  • 使用go 语言在win上截屏,可自动选择gdi和dxgi模式,也可以手动选择
  • 可设置是否绘制鼠标指针
  • 支持多显示器
  • 使用SIMD做BGRA转RGBA,提升效率
  • 分辨率不变化的情况不释放内存,以优化gdi的截图时间
  • 这是为后面要实施的远程控制和投屏软件准备的基础库

example

  • sample program main.go

    package main
    
    import (
    "fmt"
    "image"
    "image/png"
    "os"
    "runtime"
    "screenshot"
    "strconv"
    "time"
    )
    
    // 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() {
        runtime.LockOSThread()
        var err error
        shot := screenshot.NewScreenShot(1)
        if err != nil {
            fmt.Println(err)
        return
        }
    shot.DrawCursor(1)
    fmt.Println(shot.GetCaptureName())
    err = shot.Init(0)
    if err != nil {
        fmt.Println(err)
    return
    }
    defer shot.Release()
    start := time.Now()
    for i := 0; i < 10; i++ {
        start = time.Now()
        img, err := shot.Capture()
        //_, err := screenshot.Capture(0, 0, 1920, 1080)
        if err != nil {
            fmt.Println(err)
            //return
        } else {
        fmt.Println(time.Since(start))
        save(img, strconv.FormatInt(time.Now().UnixNano(), 10)+".png")
        }
        //time.Sleep(time.Millisecond * 30)
    }
        fmt.Println(time.Since(start))
    }
    

Documentation

Overview

Package screenshot captures screen-shot image as image.RGBA. Mac, Windows, Linux, FreeBSD, OpenBSD, NetBSD, and Solaris are supported.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func Capture

func Capture(x, y, width, height int) (*image.RGBA, error)

Capture returns screen capture of specified desktop region. x and y represent distance from the upper-left corner of main display. Y-axis is downward direction. This means coordinates system is similar to Windows OS.

func CaptureDisplay

func CaptureDisplay(displayIndex int) (*image.RGBA, error)

CaptureDisplay captures whole region of displayIndex'th display.

func CaptureRect

func CaptureRect(rect image.Rectangle) (*image.RGBA, error)

CaptureRect captures specified region of desktop.

func DrawIcon

func DrawIcon(hDC win.HDC, x, y int, hIcon win.HANDLE) bool

func GetCursorInfo

func GetCursorInfo(pCursorInfo *CURSORINFO) bool

func GetDisplayBounds

func GetDisplayBounds(displayIndex int) image.Rectangle

GetDisplayBounds returns the bounds of displayIndex'th display. The main display is displayIndex = 0.

func NumActiveDisplays

func NumActiveDisplays() int

NumActiveDisplays returns the number of active displays.

func ScreenToClient

func ScreenToClient(hwnd win.HWND, x, y int) (X, Y int, ok bool)

Types

type CaptureProvider

type CaptureProvider int32
const (
	ProviderAuto CaptureProvider = 0
	ProviderGDI  CaptureProvider = 1
	ProviderDXGI CaptureProvider = 2
)

type DWORD

type DWORD uint32

type DXGIScreenshot

type DXGIScreenshot struct {
	// contains filtered or unexported fields
}

func NewDXGIScreenshot

func NewDXGIScreenshot() *DXGIScreenshot

func (*DXGIScreenshot) Capture

func (s *DXGIScreenshot) Capture() (*image.RGBA, error)

func (*DXGIScreenshot) CaptureBGRA

func (s *DXGIScreenshot) CaptureBGRA() (*image.RGBA, error)

func (*DXGIScreenshot) DrawCursor

func (s *DXGIScreenshot) DrawCursor(cursor int32)

func (*DXGIScreenshot) GetCaptureName

func (s *DXGIScreenshot) GetCaptureName() string

func (*DXGIScreenshot) GetDisplayId

func (s *DXGIScreenshot) GetDisplayId() int

func (*DXGIScreenshot) Init

func (s *DXGIScreenshot) Init(display int) error

func (*DXGIScreenshot) Release

func (s *DXGIScreenshot) Release()

type HCURSOR

type HCURSOR win.HANDLE

type ScreenShot

type ScreenShot interface {
	// Init 初始化一个截屏实例,display是第几个屏幕,默认屏幕是0
	Init(display int) error
	// GetDisplayId 获取当前实例所截图的屏幕编号
	GetDisplayId() int
	// GetCaptureName 当前截图方法,GDI、DXGI
	GetCaptureName() string
	// Capture 截一张屏幕全图并用swizzle尝试使用simd方式转化为RGBA
	Capture() (*image.RGBA, error)
	// CaptureBGRA 截一张屏幕全图,不转换直接输出BGRA颜色格式
	CaptureBGRA() (*image.RGBA, error)
	// Release 释放资源,注意实例使用完毕一定要主动释放资源否则将内存溢出
	Release()
	// DrawCursor 是否绘制鼠标指针,0不绘制,1绘制,可动态设置
	DrawCursor(cursor int32)
}

func NewScreenShot

func NewScreenShot(Provider CaptureProvider) ScreenShot

Directories

Path Synopsis
internal
Package swizzle provides functions for converting between RGBA pixel formats.
Package swizzle provides functions for converting between RGBA pixel formats.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL