|
3 | 3 | #include <stdlib.h> /* malloc() */
|
4 | 4 |
|
5 | 5 | MMRect getScaledRect(MMRect input, HDC imageSource) {
|
6 |
| - BITMAP structBitmapHeader; |
7 |
| - memset( &structBitmapHeader, 0, sizeof(BITMAP) ); |
8 |
| - |
9 |
| - HGDIOBJ hBitmap = GetCurrentObject(imageSource, OBJ_BITMAP); |
10 |
| - GetObject(hBitmap, sizeof(BITMAP), &structBitmapHeader); |
11 |
| - |
12 | 6 | size_t desktopWidth = (size_t)GetSystemMetrics(SM_CXSCREEN);
|
13 | 7 | size_t desktopHeight = (size_t)GetSystemMetrics(SM_CYSCREEN);
|
14 | 8 |
|
15 |
| - double scaleX = (double)(structBitmapHeader.bmWidth / desktopWidth); |
16 |
| - double scaleY = (double)(structBitmapHeader.bmHeight / desktopHeight); |
| 9 | + double scaleX = (double)(desktopWidth / (double)input.size.width); |
| 10 | + double scaleY = (double)(desktopHeight / (double)input.size.height); |
17 | 11 |
|
18 | 12 | return MMRectMake(input.origin.x, input.origin.y, input.size.width * scaleX, input.size.height * scaleY);
|
19 | 13 | }
|
20 | 14 |
|
21 | 15 | MMBitmapRef copyMMBitmapFromDisplayInRect(MMRect rect)
|
22 | 16 | {
|
| 17 | + // Configure DPI awareness to fetch unscaled display size |
| 18 | + DPI_AWARENESS_CONTEXT initialDpiAwareness = SetThreadDpiAwarenessContext(DPI_AWARENESS_CONTEXT_PER_MONITOR_AWARE_V2); |
23 | 19 | MMBitmapRef bitmap;
|
24 | 20 | void *data;
|
25 | 21 | HDC screen = NULL, screenMem = NULL;
|
@@ -84,6 +80,8 @@ MMBitmapRef copyMMBitmapFromDisplayInRect(MMRect rect)
|
84 | 80 | ReleaseDC(NULL, screen);
|
85 | 81 | DeleteObject(dib);
|
86 | 82 | DeleteDC(screenMem);
|
| 83 | + // Reset DPI awareness to avoid inconsistencies on future calls to copyMMBitmapFromDisplayInRect |
| 84 | + SetThreadDpiAwarenessContext(initialDpiAwareness); |
87 | 85 |
|
88 | 86 | return bitmap;
|
89 | 87 | }
|
0 commit comments