Skip to content

Commit 72e3f68

Browse files
committed
Allow handle to be supplied for ImageGrab.grab() on Windows
1 parent 16372dd commit 72e3f68

File tree

4 files changed

+40
-6
lines changed

4 files changed

+40
-6
lines changed

Tests/test_imagegrab.py

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -57,6 +57,11 @@ def test_grab_invalid_xdisplay(self) -> None:
5757
ImageGrab.grab(xdisplay="error.test:0.0")
5858
assert str(e.value).startswith("X connection failed")
5959

60+
@pytest.mark.skipif(sys.platform != "win32", reason="Windows only")
61+
def test_grab_invalid_handle(self) -> None:
62+
with pytest.raises(OSError):
63+
ImageGrab.grab(handle=-1)
64+
6065
def test_grabclipboard(self) -> None:
6166
if sys.platform == "darwin":
6267
subprocess.call(["screencapture", "-cx"])

docs/reference/ImageGrab.rst

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,11 @@ or the clipboard to a PIL image memory.
3939
You can check X11 support using :py:func:`PIL.features.check_feature` with ``feature="xcb"``.
4040

4141
.. versionadded:: 7.1.0
42+
43+
:param handle:
44+
HDC, to capture a single window. Windows only.
45+
46+
.. versionadded:: 11.1.0
4247
:return: An image
4348

4449
.. py:function:: grabclipboard()

src/PIL/ImageGrab.py

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,15 +22,20 @@
2222
import subprocess
2323
import sys
2424
import tempfile
25+
from typing import TYPE_CHECKING
2526

2627
from . import Image
2728

29+
if TYPE_CHECKING:
30+
from . import ImageWin
31+
2832

2933
def grab(
3034
bbox: tuple[int, int, int, int] | None = None,
3135
include_layered_windows: bool = False,
3236
all_screens: bool = False,
3337
xdisplay: str | None = None,
38+
handle: int | ImageWin.HDC | None = None,
3439
) -> Image.Image:
3540
im: Image.Image
3641
if xdisplay is None:
@@ -51,8 +56,12 @@ def grab(
5156
return im_resized
5257
return im
5358
elif sys.platform == "win32":
59+
if handle is not None:
60+
all_screens = -1
5461
offset, size, data = Image.core.grabscreen_win32(
55-
include_layered_windows, all_screens
62+
include_layered_windows,
63+
all_screens,
64+
int(handle) if handle is not None else 0,
5665
)
5766
im = Image.frombytes(
5867
"RGB",

src/display.c

Lines changed: 20 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -320,8 +320,8 @@ typedef HANDLE(__stdcall *Func_SetThreadDpiAwarenessContext)(HANDLE);
320320

321321
PyObject *
322322
PyImaging_GrabScreenWin32(PyObject *self, PyObject *args) {
323-
int x = 0, y = 0, width, height;
324-
int includeLayeredWindows = 0, all_screens = 0;
323+
int x = 0, y = 0, width = -1, height;
324+
int includeLayeredWindows = 0, screens = 0;
325325
HBITMAP bitmap;
326326
BITMAPCOREHEADER core;
327327
HDC screen, screen_copy;
@@ -331,14 +331,18 @@ PyImaging_GrabScreenWin32(PyObject *self, PyObject *args) {
331331
HMODULE user32;
332332
Func_SetThreadDpiAwarenessContext SetThreadDpiAwarenessContext_function;
333333

334-
if (!PyArg_ParseTuple(args, "|ii", &includeLayeredWindows, &all_screens)) {
334+
if (!PyArg_ParseTuple(
335+
args, "|ii" F_HANDLE, &includeLayeredWindows, &screens, &screen
336+
)) {
335337
return NULL;
336338
}
337339

338340
/* step 1: create a memory DC large enough to hold the
339341
entire screen */
340342

341-
screen = CreateDC("DISPLAY", NULL, NULL, NULL);
343+
if (screens != -1) {
344+
screen = CreateDC("DISPLAY", NULL, NULL, NULL);
345+
}
342346
screen_copy = CreateCompatibleDC(screen);
343347

344348
// added in Windows 10 (1607)
@@ -351,11 +355,18 @@ PyImaging_GrabScreenWin32(PyObject *self, PyObject *args) {
351355
dpiAwareness = SetThreadDpiAwarenessContext_function((HANDLE)-3);
352356
}
353357

354-
if (all_screens) {
358+
if (screens == 1) {
355359
x = GetSystemMetrics(SM_XVIRTUALSCREEN);
356360
y = GetSystemMetrics(SM_YVIRTUALSCREEN);
357361
width = GetSystemMetrics(SM_CXVIRTUALSCREEN);
358362
height = GetSystemMetrics(SM_CYVIRTUALSCREEN);
363+
} else if (screens == -1) {
364+
RECT rect;
365+
HWND hwnd = WindowFromDC(screen);
366+
if (hwnd != NULL && GetClientRect(hwnd, &rect)) {
367+
width = rect.right;
368+
height = rect.bottom;
369+
}
359370
} else {
360371
width = GetDeviceCaps(screen, HORZRES);
361372
height = GetDeviceCaps(screen, VERTRES);
@@ -367,6 +378,10 @@ PyImaging_GrabScreenWin32(PyObject *self, PyObject *args) {
367378

368379
FreeLibrary(user32);
369380

381+
if (width == -1) {
382+
goto error;
383+
}
384+
370385
bitmap = CreateCompatibleBitmap(screen, width, height);
371386
if (!bitmap) {
372387
goto error;

0 commit comments

Comments
 (0)