From cacbdc680c22c5290e9bd62fab1017a19bf34e5d Mon Sep 17 00:00:00 2001
From: nulano <nulano@nulano.eu>
Date: Thu, 25 Jul 2019 21:34:58 +0200
Subject: [PATCH 1/2] override Win10 dpi scaling in screengrab

(cherry picked from commit 45ead62d6431c8339613e8ced85b705c80a7fdc9)
---
 src/display.c | 21 +++++++++++++++++++++
 1 file changed, 21 insertions(+)

diff --git a/src/display.c b/src/display.c
index ab005d4b4..8611f3778 100644
--- a/src/display.c
+++ b/src/display.c
@@ -319,6 +319,8 @@ PyImaging_DisplayModeWin32(PyObject* self, PyObject* args)
 /* -------------------------------------------------------------------- */
 /* Windows screen grabber */
 
+typedef HANDLE(__stdcall* Func_SetThreadDpiAwarenessContext)(HANDLE);
+
 PyObject*
 PyImaging_GrabScreenWin32(PyObject* self, PyObject* args)
 {
@@ -329,6 +331,9 @@ PyImaging_GrabScreenWin32(PyObject* self, PyObject* args)
     HDC screen, screen_copy;
     DWORD rop;
     PyObject* buffer;
+    HANDLE dpiAwareness;
+	HMODULE user32 = LoadLibraryA("User32.dll");
+	Func_SetThreadDpiAwarenessContext SetThreadDpiAwarenessContext_function;
 
     if (!PyArg_ParseTuple(args, "|i", &includeLayeredWindows))
         return NULL;
@@ -339,9 +344,25 @@ PyImaging_GrabScreenWin32(PyObject* self, PyObject* args)
     screen = CreateDC("DISPLAY", NULL, NULL, NULL);
     screen_copy = CreateCompatibleDC(screen);
 
+    // added in Windows 10 (1607)
+    // loaded dynamically to avoid link errors
+    SetThreadDpiAwarenessContext_function =
+            (Func_SetThreadDpiAwarenessContext)
+            GetProcAddress(user32, "SetThreadDpiAwarenessContext");
+    if (SetThreadDpiAwarenessContext_function != NULL) {
+        // DPI_AWARENESS_CONTEXT_PER_MONITOR_AWARE = ((DPI_CONTEXT_HANDLE)-3)
+        dpiAwareness = SetThreadDpiAwarenessContext_function((HANDLE) -3);
+    }
+
     width = GetDeviceCaps(screen, HORZRES);
     height = GetDeviceCaps(screen, VERTRES);
 
+    if (SetThreadDpiAwarenessContext_function != NULL) {
+        dpiAwareness = SetThreadDpiAwarenessContext_function(dpiAwareness);
+    }
+
+    FreeLibrary(user32);
+
     bitmap = CreateCompatibleBitmap(screen, width, height);
     if (!bitmap)
         goto error;

From d46f81afba0f7f3e49911bd6c481ab2a7b8b7a52 Mon Sep 17 00:00:00 2001
From: Andrew Murray <3112309+radarhere@users.noreply.github.com>
Date: Sat, 3 Aug 2019 21:52:05 +1000
Subject: [PATCH 2/2] Windows Screengrab DPI fix improvements (#2)

* Load User32 after possible return

* Removed unused setting of variable
---
 src/display.c | 7 ++++---
 1 file changed, 4 insertions(+), 3 deletions(-)

diff --git a/src/display.c b/src/display.c
index 8611f3778..fd04804a2 100644
--- a/src/display.c
+++ b/src/display.c
@@ -332,8 +332,8 @@ PyImaging_GrabScreenWin32(PyObject* self, PyObject* args)
     DWORD rop;
     PyObject* buffer;
     HANDLE dpiAwareness;
-	HMODULE user32 = LoadLibraryA("User32.dll");
-	Func_SetThreadDpiAwarenessContext SetThreadDpiAwarenessContext_function;
+    HMODULE user32;
+    Func_SetThreadDpiAwarenessContext SetThreadDpiAwarenessContext_function;
 
     if (!PyArg_ParseTuple(args, "|i", &includeLayeredWindows))
         return NULL;
@@ -346,6 +346,7 @@ PyImaging_GrabScreenWin32(PyObject* self, PyObject* args)
 
     // added in Windows 10 (1607)
     // loaded dynamically to avoid link errors
+    user32 = LoadLibraryA("User32.dll");
     SetThreadDpiAwarenessContext_function =
             (Func_SetThreadDpiAwarenessContext)
             GetProcAddress(user32, "SetThreadDpiAwarenessContext");
@@ -358,7 +359,7 @@ PyImaging_GrabScreenWin32(PyObject* self, PyObject* args)
     height = GetDeviceCaps(screen, VERTRES);
 
     if (SetThreadDpiAwarenessContext_function != NULL) {
-        dpiAwareness = SetThreadDpiAwarenessContext_function(dpiAwareness);
+        SetThreadDpiAwarenessContext_function(dpiAwareness);
     }
 
     FreeLibrary(user32);