登录
首页 >  Golang >  Go教程

go语言实现屏幕截图的示例代码

来源:脚本之家

时间:2022-12-24 17:09:02 295浏览 收藏

在Golang实战开发的过程中,我们经常会遇到一些这样那样的问题,然后要卡好半天,等问题解决了才发现原来一些细节知识点还是没有掌握好。今天golang学习网就整理分享《go语言实现屏幕截图的示例代码》,聊聊go语言屏幕截图,希望可以帮助到正在努力赚钱的你。

借助第三方库

https://github.com/kbinani/screenshot

安装

go get github.com/kbinani/screenshot

方法

详情查看
https://pkg.go.dev/github.com/vova616/screenshot

自定义截图 Capture

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

返回指定桌面区域的屏幕截图。x,y是起点坐标, width,height 是图片的宽和高。

全屏截图 CaptureDisplay

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

返回全屏截图。 displayIndex 是显示编号,默认屏幕是0,如果外接多个显示,则是1,2,3,4 … 。

获取活动显示器数量 NumActiveDisplays

func NumActiveDisplays()int

返回活动的显示器的数量。

获取指定屏幕显示范围 GetDisplayBounds

func GetDisplayBounds(displayIndex int) image.Rectangle

GetDisplayBounds返回displayIndex的显示范围, 范围从(0,0) 坐标开始,当前屏幕分辨率结束 ,例如:(0,0)-(1920,1080)。

获取自定义矩形区域的截图 CaptureRect

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

捕获桌面的指定区域。跟Capture类似,主要搭配GetDisplayBounds 使用。
参数是一个矩形,即两个点,一个最小点,一个最大点

演示

package main

import (
    "fmt"
    "github.com/kbinani/screenshot"
    "image"
    "image/png"
    "os"
)

// 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() {

    //自定义截图
    img, err := screenshot.Capture(0, 0, 500, 500)
    if err != nil {
        panic(err)
    }
    save(img, "自定义.png")

    //获取所有活动屏幕
    n := screenshot.NumActiveDisplays()
    if n  0 {
        for i := 0; i 

好了,本文到此结束,带大家了解了《go语言实现屏幕截图的示例代码》,希望本文对你有所帮助!关注golang学习网公众号,给大家分享更多Golang知识!

声明:本文转载于:脚本之家 如有侵犯,请联系study_golang@163.com删除
相关阅读
更多>
最新阅读
更多>
课程推荐
更多>
评论列表