| 1 |
//go:build windows |
| 2 |
|
| 3 |
package screenshot |
| 4 |
|
| 5 |
import ( |
| 6 |
"errors" |
| 7 |
"github.com/lxn/win" |
| 8 |
"image" |
| 9 |
"syscall" |
| 10 |
"unsafe" |
| 11 |
) |
| 12 |
|
| 13 |
var ( |
| 14 |
libUser32, _ = syscall.LoadLibrary("user32.dll") |
| 15 |
funcGetDesktopWindow, _ = syscall.GetProcAddress(syscall.Handle(libUser32), "GetDesktopWindow") |
| 16 |
funcEnumDisplayMonitors, _ = syscall.GetProcAddress(syscall.Handle(libUser32), "EnumDisplayMonitors") |
| 17 |
funcGetMonitorInfo, _ = syscall.GetProcAddress(syscall.Handle(libUser32), "GetMonitorInfoW") |
| 18 |
funcEnumDisplaySettings, _ = syscall.GetProcAddress(syscall.Handle(libUser32), "EnumDisplaySettingsW") |
| 19 |
) |
| 20 |
|
| 21 |
func Capture(x, y, width, height int) (*image.RGBA, error) { |
| 22 |
rect := image.Rect(0, 0, width, height) |
| 23 |
img, err := createImage(rect) |
| 24 |
if err != nil { |
| 25 |
return nil, err |
| 26 |
} |
| 27 |
|
| 28 |
hwnd := getDesktopWindow() |
| 29 |
hdc := win.GetDC(hwnd) |
| 30 |
if hdc == 0 { |
| 31 |
return nil, errors.New("GetDC failed") |
| 32 |
} |
| 33 |
defer win.ReleaseDC(hwnd, hdc) |
| 34 |
|
| 35 |
memory_device := win.CreateCompatibleDC(hdc) |
| 36 |
if memory_device == 0 { |
| 37 |
return nil, errors.New("CreateCompatibleDC failed") |
| 38 |
} |
| 39 |
defer win.DeleteDC(memory_device) |
| 40 |
|
| 41 |
bitmap := win.CreateCompatibleBitmap(hdc, int32(width), int32(height)) |
| 42 |
if bitmap == 0 { |
| 43 |
return nil, errors.New("CreateCompatibleBitmap failed") |
| 44 |
} |
| 45 |
defer win.DeleteObject(win.HGDIOBJ(bitmap)) |
| 46 |
|
| 47 |
var header win.BITMAPINFOHEADER |
| 48 |
header.BiSize = uint32(unsafe.Sizeof(header)) |
| 49 |
header.BiPlanes = 1 |
| 50 |
header.BiBitCount = 32 |
| 51 |
header.BiWidth = int32(width) |
| 52 |
header.BiHeight = int32(-height) |
| 53 |
header.BiCompression = win.BI_RGB |
| 54 |
header.BiSizeImage = 0 |
| 55 |
|
| 56 |
// GetDIBits balks at using Go memory on some systems. The MSDN example uses |
| 57 |
// GlobalAlloc, so we'll do that too. See: |
| 58 |
// https://docs.microsoft.com/en-gb/windows/desktop/gdi/capturing-an-image |
| 59 |
bitmapDataSize := uintptr(((int64(width)*int64(header.BiBitCount) + 31) / 32) * 4 * int64(height)) |
| 60 |
hmem := win.GlobalAlloc(win.GMEM_MOVEABLE, bitmapDataSize) |
| 61 |
defer win.GlobalFree(hmem) |
| 62 |
memptr := win.GlobalLock(hmem) |
| 63 |
defer win.GlobalUnlock(hmem) |
| 64 |
|
| 65 |
old := win.SelectObject(memory_device, win.HGDIOBJ(bitmap)) |
| 66 |
if old == 0 { |
| 67 |
return nil, errors.New("SelectObject failed") |
| 68 |
} |
| 69 |
defer win.SelectObject(memory_device, old) |
| 70 |
|
| 71 |
if !win.BitBlt(memory_device, 0, 0, int32(width), int32(height), hdc, int32(x), int32(y), win.SRCCOPY) { |
| 72 |
return nil, errors.New("BitBlt failed") |
| 73 |
} |
| 74 |
|
| 75 |
if win.GetDIBits(hdc, bitmap, 0, uint32(height), (*uint8)(memptr), (*win.BITMAPINFO)(unsafe.Pointer(&header)), win.DIB_RGB_COLORS) == 0 { |
| 76 |
return nil, errors.New("GetDIBits failed") |
| 77 |
} |
| 78 |
|
| 79 |
i := 0 |
| 80 |
src := uintptr(memptr) |
| 81 |
for y := 0; y < height; y++ { |
| 82 |
for x := 0; x < width; x++ { |
| 83 |
v0 := *(*uint8)(unsafe.Pointer(src)) |
| 84 |
v1 := *(*uint8)(unsafe.Pointer(src + 1)) |
| 85 |
v2 := *(*uint8)(unsafe.Pointer(src + 2)) |
| 86 |
|
| 87 |
// BGRA => RGBA, and set A to 255 |
| 88 |
img.Pix[i], img.Pix[i+1], img.Pix[i+2], img.Pix[i+3] = v2, v1, v0, 255 |
| 89 |
|
| 90 |
i += 4 |
| 91 |
src += 4 |
| 92 |
} |
| 93 |
} |
| 94 |
|
| 95 |
return img, nil |
| 96 |
} |
| 97 |
|
| 98 |
func getDesktopWindow() win.HWND { |
| 99 |
ret, _, _ := syscall.Syscall(funcGetDesktopWindow, 0, 0, 0, 0) |
| 100 |
return win.HWND(ret) |
| 101 |
} |
| 102 |
|
| 103 |
func enumDisplayMonitors(hdc win.HDC, lprcClip *win.RECT, lpfnEnum uintptr, dwData uintptr) bool { |
| 104 |
ret, _, _ := syscall.Syscall6(funcEnumDisplayMonitors, 4, |
| 105 |
uintptr(hdc), |
| 106 |
uintptr(unsafe.Pointer(lprcClip)), |
| 107 |
lpfnEnum, |
| 108 |
dwData, |
| 109 |
0, |
| 110 |
0) |
| 111 |
return int(ret) != 0 |
| 112 |
} |
| 113 |
|
| 114 |
func countupMonitorCallback(hMonitor win.HMONITOR, hdcMonitor win.HDC, lprcMonitor *win.RECT, dwData uintptr) uintptr { |
| 115 |
var count *int |
| 116 |
count = (*int)(unsafe.Pointer(dwData)) |
| 117 |
*count = *count + 1 |
| 118 |
return uintptr(1) |
| 119 |
} |
| 120 |
|
| 121 |
type getMonitorBoundsContext struct { |
| 122 |
Index int |
| 123 |
Rect win.RECT |
| 124 |
Count int |
| 125 |
} |
| 126 |
|
| 127 |
func getMonitorBoundsCallback(hMonitor win.HMONITOR, hdcMonitor win.HDC, lprcMonitor *win.RECT, dwData uintptr) uintptr { |
| 128 |
var ctx *getMonitorBoundsContext |
| 129 |
ctx = (*getMonitorBoundsContext)(unsafe.Pointer(dwData)) |
| 130 |
if ctx.Count != ctx.Index { |
| 131 |
ctx.Count = ctx.Count + 1 |
| 132 |
return uintptr(1) |
| 133 |
} |
| 134 |
|
| 135 |
if realSize := getMonitorRealSize(hMonitor); realSize != nil { |
| 136 |
ctx.Rect = *realSize |
| 137 |
} else { |
| 138 |
ctx.Rect = *lprcMonitor |
| 139 |
} |
| 140 |
|
| 141 |
return uintptr(0) |
| 142 |
} |
| 143 |
|
| 144 |
type _MONITORINFOEX struct { |
| 145 |
win.MONITORINFO |
| 146 |
DeviceName [win.CCHDEVICENAME]uint16 |
| 147 |
} |
| 148 |
|
| 149 |
const _ENUM_CURRENT_SETTINGS = 0xFFFFFFFF |
| 150 |
|
| 151 |
type _DEVMODE struct { |
| 152 |
_ [68]byte |
| 153 |
DmSize uint16 |
| 154 |
_ [6]byte |
| 155 |
DmPosition win.POINT |
| 156 |
_ [86]byte |
| 157 |
DmPelsWidth uint32 |
| 158 |
DmPelsHeight uint32 |
| 159 |
_ [40]byte |
| 160 |
} |
| 161 |
|
| 162 |
// getMonitorRealSize makes a call to GetMonitorInfo |
| 163 |
// to obtain the device name for the monitor handle |
| 164 |
// provided to the method. |
| 165 |
// |
| 166 |
// With the device name, EnumDisplaySettings is called to |
| 167 |
// obtain the current configuration for the monitor, this |
| 168 |
// information includes the real resolution of the monitor |
| 169 |
// rather than the scaled version based on DPI. |
| 170 |
// |
| 171 |
// If either handle calls fail, it will return a nil |
| 172 |
// allowing the calling method to use the bounds information |
| 173 |
// returned by EnumDisplayMonitors which may be affected |
| 174 |
// by DPI. |
| 175 |
func getMonitorRealSize(hMonitor win.HMONITOR) *win.RECT { |
| 176 |
info := _MONITORINFOEX{} |
| 177 |
info.CbSize = uint32(unsafe.Sizeof(info)) |
| 178 |
|
| 179 |
ret, _, _ := syscall.Syscall(funcGetMonitorInfo, 2, uintptr(hMonitor), uintptr(unsafe.Pointer(&info)), 0) |
| 180 |
if ret == 0 { |
| 181 |
return nil |
| 182 |
} |
| 183 |
|
| 184 |
devMode := _DEVMODE{} |
| 185 |
devMode.DmSize = uint16(unsafe.Sizeof(devMode)) |
| 186 |
|
| 187 |
if ret, _, _ := syscall.Syscall(funcEnumDisplaySettings, 3, uintptr(unsafe.Pointer(&info.DeviceName[0])), _ENUM_CURRENT_SETTINGS, uintptr(unsafe.Pointer(&devMode))); ret == 0 { |
| 188 |
return nil |
| 189 |
} |
| 190 |
|
| 191 |
return &win.RECT{ |
| 192 |
Left: devMode.DmPosition.X, |
| 193 |
Right: devMode.DmPosition.X + int32(devMode.DmPelsWidth), |
| 194 |
Top: devMode.DmPosition.Y, |
| 195 |
Bottom: devMode.DmPosition.Y + int32(devMode.DmPelsHeight), |
| 196 |
} |
| 197 |
} |