forked from vova616/screenshot
-
Notifications
You must be signed in to change notification settings - Fork 0
/
screenshot_windows.go
243 lines (201 loc) · 5.59 KB
/
screenshot_windows.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
package screenshot
import (
"fmt"
"image"
"reflect"
"syscall"
"unsafe"
)
func ScreenRect() (image.Rectangle, error) {
hDC := GetDC(0)
if hDC == 0 {
return image.Rectangle{}, fmt.Errorf("Could not Get primary display err:%d\n", GetLastError())
}
defer ReleaseDC(0, hDC)
x := GetDeviceCaps(hDC, HORZRES)
y := GetDeviceCaps(hDC, VERTRES)
return image.Rect(0, 0, x, y), nil
}
func CaptureScreen() (*image.RGBA, error) {
r, e := ScreenRect()
if e != nil {
return nil, e
}
return CaptureRect(r)
}
func CaptureRect(rect image.Rectangle) (*image.RGBA, error) {
hDC := GetDC(0)
if hDC == 0 {
return nil, fmt.Errorf("Could not Get primary display err:%d.\n", GetLastError())
}
defer ReleaseDC(0, hDC)
m_hDC := CreateCompatibleDC(hDC)
if m_hDC == 0 {
return nil, fmt.Errorf("Could not Create Compatible DC err:%d.\n", GetLastError())
}
defer DeleteDC(m_hDC)
x, y := rect.Dx(), rect.Dy()
bt := BITMAPINFO{}
bt.BmiHeader.BiSize = uint32(reflect.TypeOf(bt.BmiHeader).Size())
bt.BmiHeader.BiWidth = int32(x)
bt.BmiHeader.BiHeight = int32(-y)
bt.BmiHeader.BiPlanes = 1
bt.BmiHeader.BiBitCount = 32
bt.BmiHeader.BiCompression = BI_RGB
ptr := unsafe.Pointer(uintptr(0))
m_hBmp := CreateDIBSection(m_hDC, &bt, DIB_RGB_COLORS, &ptr, 0, 0)
if m_hBmp == 0 {
return nil, fmt.Errorf("Could not Create DIB Section err:%d.\n", GetLastError())
}
if m_hBmp == InvalidParameter {
return nil, fmt.Errorf("One or more of the input parameters is invalid while calling CreateDIBSection.\n")
}
defer DeleteObject(HGDIOBJ(m_hBmp))
obj := SelectObject(m_hDC, HGDIOBJ(m_hBmp))
if obj == 0 {
return nil, fmt.Errorf("error occurred and the selected object is not a region err:%d.\n", GetLastError())
}
if obj == 0xffffffff { //GDI_ERROR
return nil, fmt.Errorf("GDI_ERROR while calling SelectObject err:%d.\n", GetLastError())
}
defer DeleteObject(obj)
//Note:BitBlt contains bad error handling, we will just assume it works and if it doesn't it will panic :x
BitBlt(m_hDC, 0, 0, x, y, hDC, rect.Min.X, rect.Min.Y, SRCCOPY)
var slice []byte
hdrp := (*reflect.SliceHeader)(unsafe.Pointer(&slice))
hdrp.Data = uintptr(ptr)
hdrp.Len = x * y * 4
hdrp.Cap = x * y * 4
imageBytes := make([]byte, len(slice))
for i := 0; i < len(imageBytes); i += 4 {
imageBytes[i], imageBytes[i+2], imageBytes[i+1], imageBytes[i+3] = slice[i+2], slice[i], slice[i+1], slice[i+3]
}
img := &image.RGBA{imageBytes, 4 * x, image.Rect(0, 0, x, y)}
return img, nil
}
func GetDeviceCaps(hdc HDC, index int) int {
ret, _, _ := procGetDeviceCaps.Call(
uintptr(hdc),
uintptr(index))
return int(ret)
}
func GetDC(hwnd HWND) HDC {
ret, _, _ := procGetDC.Call(
uintptr(hwnd))
return HDC(ret)
}
func ReleaseDC(hwnd HWND, hDC HDC) bool {
ret, _, _ := procReleaseDC.Call(
uintptr(hwnd),
uintptr(hDC))
return ret != 0
}
func DeleteDC(hdc HDC) bool {
ret, _, _ := procDeleteDC.Call(
uintptr(hdc))
return ret != 0
}
func GetLastError() uint32 {
ret, _, _ := procGetLastError.Call()
return uint32(ret)
}
func BitBlt(hdcDest HDC, nXDest, nYDest, nWidth, nHeight int, hdcSrc HDC, nXSrc, nYSrc int, dwRop uint) {
ret, _, _ := procBitBlt.Call(
uintptr(hdcDest),
uintptr(nXDest),
uintptr(nYDest),
uintptr(nWidth),
uintptr(nHeight),
uintptr(hdcSrc),
uintptr(nXSrc),
uintptr(nYSrc),
uintptr(dwRop))
if ret == 0 {
panic("BitBlt failed")
}
}
func SelectObject(hdc HDC, hgdiobj HGDIOBJ) HGDIOBJ {
ret, _, _ := procSelectObject.Call(
uintptr(hdc),
uintptr(hgdiobj))
if ret == 0 {
panic("SelectObject failed")
}
return HGDIOBJ(ret)
}
func DeleteObject(hObject HGDIOBJ) bool {
ret, _, _ := procDeleteObject.Call(
uintptr(hObject))
return ret != 0
}
func CreateDIBSection(hdc HDC, pbmi *BITMAPINFO, iUsage uint, ppvBits *unsafe.Pointer, hSection HANDLE, dwOffset uint) HBITMAP {
ret, _, _ := procCreateDIBSection.Call(
uintptr(hdc),
uintptr(unsafe.Pointer(pbmi)),
uintptr(iUsage),
uintptr(unsafe.Pointer(ppvBits)),
uintptr(hSection),
uintptr(dwOffset))
return HBITMAP(ret)
}
func CreateCompatibleDC(hdc HDC) HDC {
ret, _, _ := procCreateCompatibleDC.Call(
uintptr(hdc))
if ret == 0 {
panic("Create compatible DC failed")
}
return HDC(ret)
}
type (
HANDLE uintptr
HWND HANDLE
HGDIOBJ HANDLE
HDC HANDLE
HBITMAP HANDLE
)
type BITMAPINFO struct {
BmiHeader BITMAPINFOHEADER
BmiColors *RGBQUAD
}
type BITMAPINFOHEADER struct {
BiSize uint32
BiWidth int32
BiHeight int32
BiPlanes uint16
BiBitCount uint16
BiCompression uint32
BiSizeImage uint32
BiXPelsPerMeter int32
BiYPelsPerMeter int32
BiClrUsed uint32
BiClrImportant uint32
}
type RGBQUAD struct {
RgbBlue byte
RgbGreen byte
RgbRed byte
RgbReserved byte
}
const (
HORZRES = 8
VERTRES = 10
BI_RGB = 0
InvalidParameter = 2
DIB_RGB_COLORS = 0
SRCCOPY = 0x00CC0020
)
var (
modgdi32 = syscall.NewLazyDLL("gdi32.dll")
moduser32 = syscall.NewLazyDLL("user32.dll")
modkernel32 = syscall.NewLazyDLL("kernel32.dll")
procGetDC = moduser32.NewProc("GetDC")
procReleaseDC = moduser32.NewProc("ReleaseDC")
procDeleteDC = modgdi32.NewProc("DeleteDC")
procBitBlt = modgdi32.NewProc("BitBlt")
procDeleteObject = modgdi32.NewProc("DeleteObject")
procSelectObject = modgdi32.NewProc("SelectObject")
procCreateDIBSection = modgdi32.NewProc("CreateDIBSection")
procCreateCompatibleDC = modgdi32.NewProc("CreateCompatibleDC")
procGetDeviceCaps = modgdi32.NewProc("GetDeviceCaps")
procGetLastError = modkernel32.NewProc("GetLastError")
)