Skip to content

(nut-tree/nut.js/183) Determine scale factor ifor high DPI displays i… #59

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Mar 17, 2021
Merged
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
44 changes: 31 additions & 13 deletions src/win32/screengrab.c
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,22 @@
#include "../endian.h"
#include <stdlib.h> /* malloc() */

MMRect getScaledRect(MMRect input, HDC imageSource) {
BITMAP structBitmapHeader;
memset( &structBitmapHeader, 0, sizeof(BITMAP) );

HGDIOBJ hBitmap = GetCurrentObject(imageSource, OBJ_BITMAP);
GetObject(hBitmap, sizeof(BITMAP), &structBitmapHeader);

size_t desktopWidth = (size_t)GetSystemMetrics(SM_CXSCREEN);
size_t desktopHeight = (size_t)GetSystemMetrics(SM_CYSCREEN);

double scaleX = (double)(structBitmapHeader.bmWidth / desktopWidth);
double scaleY = (double)(structBitmapHeader.bmHeight / desktopHeight);

return MMRectMake(input.origin.x, input.origin.y, input.size.width * scaleX, input.size.height * scaleY);
}

MMBitmapRef copyMMBitmapFromDisplayInRect(MMRect rect)
{
MMBitmapRef bitmap;
Expand All @@ -10,22 +26,24 @@ MMBitmapRef copyMMBitmapFromDisplayInRect(MMRect rect)
HBITMAP dib;
BITMAPINFO bi;

screen = GetWindowDC(NULL); /* Get entire screen */
MMRect scaledRect = getScaledRect(rect, screen);

if (screen == NULL) return NULL;

/* Initialize bitmap info. */
bi.bmiHeader.biSize = sizeof(bi.bmiHeader);
bi.bmiHeader.biWidth = (long)rect.size.width;
bi.bmiHeader.biHeight = -(long)rect.size.height; /* Non-cartesian, please */
bi.bmiHeader.biWidth = (long)scaledRect.size.width;
bi.bmiHeader.biHeight = -(long)scaledRect.size.height; /* Non-cartesian, please */
bi.bmiHeader.biPlanes = 1;
bi.bmiHeader.biBitCount = 32;
bi.bmiHeader.biCompression = BI_RGB;
bi.bmiHeader.biSizeImage = (DWORD)(4 * rect.size.width * rect.size.height);
bi.bmiHeader.biSizeImage = (DWORD)(4 * scaledRect.size.width * scaledRect.size.height);
bi.bmiHeader.biXPelsPerMeter = 0;
bi.bmiHeader.biYPelsPerMeter = 0;
bi.bmiHeader.biClrUsed = 0;
bi.bmiHeader.biClrImportant = 0;

screen = GetDC(NULL); /* Get entire screen */
if (screen == NULL) return NULL;

/* Get screen data in display device context. */
dib = CreateDIBSection(screen, &bi, DIB_RGB_COLORS, &data, NULL, 0);

Expand All @@ -35,11 +53,11 @@ MMBitmapRef copyMMBitmapFromDisplayInRect(MMRect rect)
!BitBlt(screenMem,
(int)0,
(int)0,
(int)rect.size.width,
(int)rect.size.height,
(int)scaledRect.size.width,
(int)scaledRect.size.height,
screen,
rect.origin.x,
rect.origin.y,
scaledRect.origin.x,
scaledRect.origin.y,
SRCCOPY)) {

/* Error copying data. */
Expand All @@ -51,9 +69,9 @@ MMBitmapRef copyMMBitmapFromDisplayInRect(MMRect rect)
}

bitmap = createMMBitmap(NULL,
rect.size.width,
rect.size.height,
4 * rect.size.width,
scaledRect.size.width,
scaledRect.size.height,
4 * scaledRect.size.width,
(uint8_t)bi.bmiHeader.biBitCount,
4);

Expand Down