将GDI32.dll位图保存到磁盘中的Golang代码部分:

huangapple go评论85阅读模式
英文:

Saving GDI32.dll bitmap to disk in Golang

问题

我的第一个SO问题 将GDI32.dll位图保存到磁盘中的Golang代码部分: 我希望通过调用User32.dll和GDI32.dll在Windows机器上(项目要求)来从Golang中进行屏幕截图。

我有一个包含屏幕截图像素的位图句柄。然而,我不知道如何访问其数据或将其保存到磁盘上。有人知道如何将GDI位图映射到Golang的[]byte,然后保存为JPG或PNG吗?

package main
import "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...
)

//
// 省略部分代码
//
func TakeDesktopScreenshotViaWinAPI() {

    // 这些都是对user32.dll或gdi32.dll的调用
    hDesktop := GetDesktopWindow()
    desktopRect := GetClientRect(hDesktop)
    width := int(desktopRect.Right - desktopRect.Left)
    height := int(desktopRect.Bottom - desktopRect.Top)

    // 创建设备上下文
    srcDC := GetDC(hDesktop)
    targetDC := CreateCompatibleDC(srcDC)

    // 创建要复制到的位图
    hBitmap := CreateCompatibleBitmap(targetDC, width, height)

    // 将位图选择到目标DC中
    hOldSelection := SelectObject(targetDC, HGDIOBJ(hBitmap))

    // 从源到目标进行位块传输
    BitBlt(targetDC, 0, 0, width, height, srcDC, 0, 0, SRCCOPY)

   // 如何保存*hBitmap中的数据 ???

   // 恢复选择
   SelectObject(targetDC, hOldSelection)

   // 清理
   DeleteDC(HDC(targetDC))
   ReleaseDC(hDesktop, srcDC)
   DeleteObject(HGDIOBJ(hBitmap))
}
英文:

My first SO question 将GDI32.dll位图保存到磁盘中的Golang代码部分: I wish to take a screenshot from Golang by calling into User32.dll and GDI32.dll on a windows machine (project requirement).

I have a handle to the bitmap containing screenshot pixels. However, I don't know how to access its data or how to save it to disk. Anyone know how to map GDI bitmap to Golang []byte and then save as a JPG or PNG?

package main
import "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

得分: 2

你可以使用vova616的screenshot库,或者查看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()
}
英文:

You could just use the screenshot library by vova616, or take a look at the screenshot_windows.go for the required conversion method.

As per the provided example:

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()
}

huangapple
  • 本文由 发表于 2015年11月20日 02:27:21
  • 转载请务必保留本文链接:https://go.coder-hub.com/33811615.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定