網站首頁 編程語言 正文
借助第三方庫
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 { ?? ??? ?panic("沒有發現活動的顯示器") ?? ?} ?? ?//全屏截取所有活動屏幕 ?? ?if n > 0 { ?? ??? ?for i := 0; i < n; i++ { ?? ??? ??? ?img, err = screenshot.CaptureDisplay(i) ?? ??? ??? ?if err != nil { ?? ??? ??? ??? ?panic(err) ?? ??? ??? ?} ?? ??? ??? ?fileName := fmt.Sprintf("第%d屏幕截圖.png", i) ?? ??? ??? ?save(img, fileName) ?? ??? ?} ?? ?} ?? ?//使用 Rectangle 自定義截圖 ?? ?// 獲取第一個屏幕顯示范圍 ?? ?var new image.Rectangle = image.Rect(0, 0, 600, 700) ?? ?img, err = screenshot.CaptureRect(new) ?? ?if err != nil { ?? ??? ?panic(err) ?? ?} ?? ?save(img, "new.png") ?? ?//使用 GetDisplayBounds獲取指定屏幕顯示范圍,全屏截圖 ?? ?bounds := screenshot.GetDisplayBounds(0) ?? ?img, err = screenshot.CaptureRect(bounds) ?? ?if err != nil { ?? ??? ?panic(err) ?? ?} ?? ?save(img, "all.png") ?? ?//使用Union獲取指定屏幕 矩形最小點和最大點 ?? ?var all image.Rectangle = image.Rect(0, 0, 0, 0) ?? ?bounds1 := screenshot.GetDisplayBounds(0) ?? ?all = bounds1.Union(all) ?? ?fmt.Println(all.Min.X, all.Min.Y, all.Dx(), all.Dy()) }
原文鏈接:https://blog.csdn.net/yang731227/article/details/108176980
相關推薦
- 2022-03-04 在項目中 npm i報錯 npm WARN optional SKIPPING OPTIONAL D
- 2022-09-10 親自教你在netty中使用TCP協議請求DNS服務器的詳細過程_服務器其它
- 2022-05-28 C語言實現隨機抽取紙牌程序_C 語言
- 2022-07-22 mybatis源碼之集成spring原理詳解
- 2022-09-30 QT實現多文件拖拽獲取路徑的方法_C 語言
- 2022-07-03 python類的私有屬性和公共屬性說明_python
- 2022-03-19 關于docker中?WSL?配置與修改問題_docker
- 2022-08-18 Redis7.0部署集群的實現步驟_Redis
- 最近更新
-
- window11 系統安裝 yarn
- 超詳細win安裝深度學習環境2025年最新版(
- Linux 中運行的top命令 怎么退出?
- MySQL 中decimal 的用法? 存儲小
- get 、set 、toString 方法的使
- @Resource和 @Autowired注解
- Java基礎操作-- 運算符,流程控制 Flo
- 1. Int 和Integer 的區別,Jav
- spring @retryable不生效的一種
- Spring Security之認證信息的處理
- Spring Security之認證過濾器
- Spring Security概述快速入門
- Spring Security之配置體系
- 【SpringBoot】SpringCache
- Spring Security之基于方法配置權
- redisson分布式鎖中waittime的設
- maven:解決release錯誤:Artif
- restTemplate使用總結
- Spring Security之安全異常處理
- MybatisPlus優雅實現加密?
- Spring ioc容器與Bean的生命周期。
- 【探索SpringCloud】服務發現-Nac
- Spring Security之基于HttpR
- Redis 底層數據結構-簡單動態字符串(SD
- arthas操作spring被代理目標對象命令
- Spring中的單例模式應用詳解
- 聊聊消息隊列,發送消息的4種方式
- bootspring第三方資源配置管理
- GIT同步修改后的遠程分支