You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Go调用w32通过窗口句柄截图仅显示黑白的问题求助

Go调用w32库窗口截图黑白问题求助

用Go语言调用w32库,通过窗口句柄对目标窗口执行截图操作时,生成的截图仅显示黑白画面,无法得到正常彩色截图。尝试修改像素处理逻辑后,问题反而加剧。

实现代码

package main

import (
    "fmt"
    "github.com/gonutz/w32/v2"
    "image"
    "image/color"
    "image/png"
    "log"
    "os"
    "unsafe"
)

func TakeScreenshot(hWnd w32.HWND, fileName string) {
    fmt.Println(fmt.Sprintf("HWND: %d", hWnd))
    rect := w32.GetWindowRect(hWnd)
    width := rect.Right - rect.Left
    height := rect.Bottom - rect.Top
    fmt.Println(width, height)

    hdc := w32.GetWindowDC(hWnd)
    defer w32.ReleaseDC(hWnd, hdc)

    dcDest := w32.CreateCompatibleDC(hdc)
    defer w32.DeleteDC(dcDest)

    hBitmap := w32.CreateCompatibleBitmap(dcDest, int(width), int(height))
    defer w32.DeleteObject(w32.HGDIOBJ(hBitmap))

    bitmapInfo := w32.BITMAPINFO{
        BmiHeader: w32.BITMAPINFOHEADER{
            BiSize:        uint32(unsafe.Sizeof(w32.BITMAPINFOHEADER{})),
            BiPlanes:      1,
            BiBitCount:    32,
            BiWidth:       width,
            BiHeight:      -height,
            BiCompression: w32.BI_RGB,
            BiSizeImage:   uint32(width * height * 4),
        },
    }
    hOld := w32.SelectObject(dcDest, w32.HGDIOBJ(hBitmap))
    w32.BitBlt(dcDest, 0, 0, int(width), int(height), hdc, 0, 0, w32.SRCCOPY)
    w32.SelectObject(dcDest, hOld)

    pixels := make([]byte, width*height*4)
    w32.GetDIBits(hdc, hBitmap, 0, uint(height), unsafe.Pointer(&pixels[0]), &bitmapInfo, w32.DIB_RGB_COLORS)

    img := image.NewRGBA(image.Rect(0, 0, int(width), int(height)))
    for y := 0; y < int(height); y++ {
        for x := 0; x < int(width); x++ {
            offset := (y*int(width) + x) * 4
            r := pixels[offset+2]
            g := pixels[offset+1]
            b := pixels[offset]
            img.SetRGBA(x, y, color.RGBA{R: r, G: g, B: b, A: 255})
        }
    }
    file, err := os.OpenFile(fileName, os.O_WRONLY|os.O_CREATE, 0777)
    if err != nil {
        log.Fatal(err)
    }
    defer func(file *os.File) {
        err := file.Close()
        if err != nil {
            fmt.Println(err)
        }
    }(file)

    if err = png.Encode(file, img); err != nil {
        log.Fatal("ERROR SAVING IMAGE: ", err)
    }
}

func main() {
    hWnd := w32.FindWindow("WTWindow", "")
    if hWnd == 0 {
        fmt.Println("Window find err")
        return
    }
    TakeScreenshot(hWnd, "./screenshot.png")
}

执行后截图为黑白画面,尝试修改以下像素处理代码后,问题反而加剧:

for y := 0; y < int(height); y++ {
    for x := 0; x < int(width); x++ {
        offset := (y*int(width) + x) * 4
        r := pixels[offset+2]
        g := pixels[offset+1]
        b := pixels[offset]
        img.SetRGBA(x, y, color.RGBA{R: r, G: g, B: b, A: 255})
    }
}

恳请技术人士提供帮助,非常感谢!


内容的提问来源于stack exchange,提问作者Strawberry

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.07.17 07:37:20