-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathwin-screenshot
More file actions
executable file
·234 lines (195 loc) · 7.92 KB
/
Copy pathwin-screenshot
File metadata and controls
executable file
·234 lines (195 loc) · 7.92 KB
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
#!/usr/bin/env bash
# Capture the full Windows desktop from a WSL checkout and save it as a PNG.
# The implementation shells out to Windows PowerShell because Linux screenshot
# tools inside WSL cannot see the host Windows desktop.
set -euo pipefail
# Print command usage for humans and for failed argument validation.
usage() {
cat <<'USAGE'
Usage: scripts/win-screenshot [output.png]
Capture the full Windows virtual desktop from WSL and save it as a PNG.
If output.png is omitted, a timestamped file is written to the current directory.
Requirements:
- Windows 11 host
- WSL with powershell.exe and wslpath available on PATH
USAGE
}
if [[ "${1:-}" == "-h" || "${1:-}" == "--help" ]]; then
usage
exit 0
fi
if [[ $# -gt 1 ]]; then
usage >&2
exit 2
fi
if ! command -v powershell.exe >/dev/null 2>&1; then
echo "win-screenshot: powershell.exe is not available on PATH" >&2
exit 1
fi
if ! command -v wslpath >/dev/null 2>&1; then
echo "win-screenshot: wslpath is not available" >&2
exit 1
fi
if [[ $# -eq 1 ]]; then
output=$1
else
output="desktop-screenshot-$(date +%Y%m%d-%H%M%S).png"
fi
case "$output" in
/*) ;;
*) output="$PWD/$output" ;;
esac
parent=$(dirname "$output")
mkdir -p "$parent"
parent=$(cd "$parent" && pwd -P)
output="$parent/$(basename "$output")"
windows_output=$(wslpath -w "$output")
# WIN_SCREENSHOT_OUT crosses from WSL into powershell.exe through WSLENV. The
# value is already a Windows path, so it should not use WSLENV's path conversion.
WIN_SCREENSHOT_OUT="$windows_output" \
WSLENV="${WSLENV:+$WSLENV:}WIN_SCREENSHOT_OUT" \
powershell.exe -NoProfile -ExecutionPolicy Bypass -Command '
$ErrorActionPreference = "Stop"
$path = [Environment]::GetEnvironmentVariable("WIN_SCREENSHOT_OUT")
if ([string]::IsNullOrWhiteSpace($path)) {
throw "WIN_SCREENSHOT_OUT was not passed from WSL."
}
Add-Type -TypeDefinition @"
using System;
using System.ComponentModel;
using System.Drawing;
using System.Drawing.Imaging;
using System.Runtime.InteropServices;
// WslDesktopScreenshot owns the Windows-only capture routine used by the WSL
// wrapper. It calls the Win32 desktop device context directly so the capture
// covers the full virtual desktop, including shell surfaces such as the taskbar.
public static class WslDesktopScreenshot
{
private const int SM_XVIRTUALSCREEN = 76;
private const int SM_YVIRTUALSCREEN = 77;
private const int SM_CXVIRTUALSCREEN = 78;
private const int SM_CYVIRTUALSCREEN = 79;
private const int SRCCOPY = 0x00CC0020;
private const int CAPTUREBLT = 0x40000000;
// Read monitor metrics from Windows so multi-monitor and taskbar regions are
// captured by desktop bounds rather than by the active window bounds.
[DllImport("user32.dll")]
private static extern int GetSystemMetrics(int nIndex);
// Ask Windows to report physical desktop pixels instead of DPI-virtualized
// coordinates when the host allows this process-level setting.
[DllImport("user32.dll")]
private static extern bool SetProcessDPIAware();
// Open the desktop device context; IntPtr.Zero addresses the whole desktop.
[DllImport("user32.dll")]
private static extern IntPtr GetDC(IntPtr hWnd);
// Release the desktop device context after the bitmap has been copied.
[DllImport("user32.dll")]
private static extern int ReleaseDC(IntPtr hWnd, IntPtr hDC);
// Create an in-memory drawing target compatible with the desktop context.
[DllImport("gdi32.dll", SetLastError = true)]
private static extern IntPtr CreateCompatibleDC(IntPtr hdc);
// Dispose the in-memory drawing target once capture is complete.
[DllImport("gdi32.dll", SetLastError = true)]
private static extern bool DeleteDC(IntPtr hdc);
// Allocate the bitmap that receives the copied desktop pixels.
[DllImport("gdi32.dll", SetLastError = true)]
private static extern IntPtr CreateCompatibleBitmap(IntPtr hdc, int width, int height);
// Select the destination bitmap into the memory context before copying.
[DllImport("gdi32.dll", SetLastError = true)]
private static extern IntPtr SelectObject(IntPtr hdc, IntPtr gdiObject);
// Dispose the native bitmap handle after saving the managed Bitmap.
[DllImport("gdi32.dll", SetLastError = true)]
private static extern bool DeleteObject(IntPtr gdiObject);
// Copy pixels from the desktop context into the memory context. CAPTUREBLT is
// supplied by the caller so layered shell windows are included.
[DllImport("gdi32.dll", SetLastError = true)]
private static extern bool BitBlt(
IntPtr destinationDeviceContext,
int destinationX,
int destinationY,
int width,
int height,
IntPtr sourceDeviceContext,
int sourceX,
int sourceY,
int rasterOperation);
// Capture saves a PNG of the full Windows virtual desktop to the path
// supplied by the WSL wrapper. It throws Win32Exception with the native error
// code when Windows cannot open or copy the desktop surface.
public static void Capture(string path)
{
try
{
SetProcessDPIAware();
}
catch
{
// DPI awareness only improves monitor metrics. Capture can still work
// when Windows rejects the call because the process is already aware.
}
int left = GetSystemMetrics(SM_XVIRTUALSCREEN);
int top = GetSystemMetrics(SM_YVIRTUALSCREEN);
int width = GetSystemMetrics(SM_CXVIRTUALSCREEN);
int height = GetSystemMetrics(SM_CYVIRTUALSCREEN);
if (width <= 0 || height <= 0)
{
throw new InvalidOperationException(
"Windows reported an invalid virtual desktop size: " + width + "x" + height + ".");
}
IntPtr desktopDeviceContext = GetDC(IntPtr.Zero);
if (desktopDeviceContext == IntPtr.Zero)
{
throw new Win32Exception(Marshal.GetLastWin32Error(), "Could not open the desktop device context.");
}
IntPtr memoryDeviceContext = IntPtr.Zero;
IntPtr bitmapHandle = IntPtr.Zero;
IntPtr previousObject = IntPtr.Zero;
try
{
memoryDeviceContext = CreateCompatibleDC(desktopDeviceContext);
if (memoryDeviceContext == IntPtr.Zero)
{
throw new Win32Exception(Marshal.GetLastWin32Error(), "Could not create a memory device context.");
}
bitmapHandle = CreateCompatibleBitmap(desktopDeviceContext, width, height);
if (bitmapHandle == IntPtr.Zero)
{
throw new Win32Exception(Marshal.GetLastWin32Error(), "Could not create the screenshot bitmap.");
}
previousObject = SelectObject(memoryDeviceContext, bitmapHandle);
if (previousObject == IntPtr.Zero)
{
throw new Win32Exception(Marshal.GetLastWin32Error(), "Could not select the screenshot bitmap.");
}
int operation = SRCCOPY | CAPTUREBLT;
if (!BitBlt(memoryDeviceContext, 0, 0, width, height, desktopDeviceContext, left, top, operation))
{
throw new Win32Exception(Marshal.GetLastWin32Error(), "Could not copy the full virtual desktop.");
}
using (Bitmap bitmap = Image.FromHbitmap(bitmapHandle))
{
bitmap.Save(path, ImageFormat.Png);
}
}
finally
{
if (previousObject != IntPtr.Zero)
{
SelectObject(memoryDeviceContext, previousObject);
}
if (bitmapHandle != IntPtr.Zero)
{
DeleteObject(bitmapHandle);
}
if (memoryDeviceContext != IntPtr.Zero)
{
DeleteDC(memoryDeviceContext);
}
ReleaseDC(IntPtr.Zero, desktopDeviceContext);
}
}
}
"@ -ReferencedAssemblies System.Drawing
[WslDesktopScreenshot]::Capture($path)
'
printf '%s\n' "$output"