为了账号安全,请及时绑定邮箱和手机立即绑定

在 Golang 中将 GDI32.dll 位图保存到磁盘

在 Golang 中将 GDI32.dll 位图保存到磁盘

Go
白板的微信 2021-11-22 18:20:03
我的第一个 SO 问题 :-) 我希望通过调用 Windows 机器上的 User32.dll 和 GDI32.dll(项目要求)从 Golang 截取屏幕截图。我有一个包含屏幕截图像素的位图的句柄。但是,我不知道如何访问其数据或如何将其保存到磁盘。任何人都知道如何将 GDI 位图映射到 Golang []byte 然后另存为 JPG 或 PNG?package mainimport "syscall"var (    user32            = syscall.NewLazyDLL("user32.dll")    procGetClientRect = user32.NewProc("GetClientRect")    // etc...    gdi32             = syscall.NewLazyDLL("gdi32.dll")    procCreateDC      = gdi32.NewProc("CreateDC")    SRCCOPY      uint = 13369376   //etc...)//// omitted for brevity//func TakeDesktopScreenshotViaWinAPI() {    // these are all calls to user32.dll or gdi32.dll    hDesktop := GetDesktopWindow()    desktopRect := GetClientRect(hDesktop)    width := int(desktopRect.Right - desktopRect.Left)    height := int(desktopRect.Bottom - desktopRect.Top)    // create device contexts    srcDC := GetDC(hDesktop)    targetDC := CreateCompatibleDC(srcDC)    // create bitmap to copy to    hBitmap := CreateCompatibleBitmap(targetDC, width, height)    // select the bitmap into target DC    hOldSelection := SelectObject(targetDC, HGDIOBJ(hBitmap))    //bit block transfer from src to target    BitBlt(targetDC, 0, 0, width, height, srcDC, 0, 0, SRCCOPY)   // how to save the  the data in   // *hBitmap ???   // restore selection   SelectObject(targetDC, hOldSelection)   // clean up   DeleteDC(HDC(targetDC))   ReleaseDC(hDesktop, srcDC)   DeleteObject(HGDIOBJ(hBitmap))}
查看完整描述

1 回答

?
繁星点点滴滴

TA贡献1803条经验 获得超3个赞

您可以使用vova616的屏幕截图库,或者查看screenshot_windows.go以了解所需的转换方法。


根据提供的示例:


package main


import (

    "github.com/vova616/screenshot"

    "image/png"

    "os"

)


func main() {

    img, err := screenshot.CaptureScreen()

    if err != nil {

        panic(err)

    }

    f, err := os.Create("./ss.png")

    if err != nil {

        panic(err)

    }

    err = png.Encode(f, img)

    if err != nil {

        panic(err)

    }

    f.Close()

}


查看完整回答
反对 回复 2021-11-22

添加回答

代码语言

举报

0/150
提交
取消
微信客服

购课补贴
联系客服咨询优惠详情

帮助反馈 APP下载

慕课网APP
您的移动学习伙伴

公众号

扫描二维码
关注慕课网微信公众号