Skip to content

(#92) Fix application of scale factor when calculating actual image s… #93

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
Dec 28, 2021
Merged
Changes from all commits
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
61 changes: 34 additions & 27 deletions src/win32/screengrab.c
Original file line number Diff line number Diff line change
@@ -2,7 +2,8 @@
#include "../endian.h"
#include <stdlib.h> /* malloc() */

MMRect getScaledRect(MMRect input, HDC imageSource) {
MMRect getScaledRect(MMRect input)
{
// Configure DPI awareness to fetch unscaled display size
DPI_AWARENESS_CONTEXT initialDpiAwareness = SetThreadDpiAwarenessContext(DPI_AWARENESS_CONTEXT_PER_MONITOR_AWARE_V2);
size_t scaledDesktopWidth = (size_t)GetSystemMetrics(SM_CXSCREEN);
@@ -15,7 +16,7 @@ MMRect getScaledRect(MMRect input, HDC imageSource) {
double scaleX = (double)(desktopWidth / (double)scaledDesktopWidth);
double scaleY = (double)(desktopHeight / (double)scaledDesktopHeight);

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

MMBitmapRef copyMMBitmapFromDisplayInRect(MMRect rect)
@@ -27,56 +28,62 @@ MMBitmapRef copyMMBitmapFromDisplayInRect(MMRect rect)
BITMAPINFO bi;

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

if (screen == NULL) return NULL;
if (screen == NULL) {
return NULL;
}

/* Initialize bitmap info. */
bi.bmiHeader.biSize = sizeof(bi.bmiHeader);
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 * scaledRect.size.width * scaledRect.size.height);
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 * scaledRect.size.width * scaledRect.size.height);
bi.bmiHeader.biXPelsPerMeter = 0;
bi.bmiHeader.biYPelsPerMeter = 0;
bi.bmiHeader.biClrUsed = 0;
bi.bmiHeader.biClrImportant = 0;

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

/* Copy the data into a bitmap struct. */
if ((screenMem = CreateCompatibleDC(screen)) == NULL ||
SelectObject(screenMem, dib) == NULL ||
!BitBlt(screenMem,
(int)0,
(int)0,
(int)scaledRect.size.width,
(int)scaledRect.size.height,
screen,
(int)scaledRect.origin.x,
(int)scaledRect.origin.y,
SRCCOPY)) {

(int)0,
(int)0,
(int)scaledRect.size.width,
(int)scaledRect.size.height,
screen,
(int)scaledRect.origin.x,
(int)scaledRect.origin.y,
SRCCOPY))
{

/* Error copying data. */
ReleaseDC(NULL, screen);
DeleteObject(dib);
if (screenMem != NULL) DeleteDC(screenMem);
if (screenMem != NULL) {
DeleteDC(screenMem);
}

return NULL;
}

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

/* Copy the data to our pixel buffer. */
if (bitmap != NULL) {
if (bitmap != NULL)
{
bitmap->imageBuffer = malloc(bitmap->bytewidth * bitmap->height);
memcpy(bitmap->imageBuffer, data, bitmap->bytewidth * bitmap->height);
}
2 changes: 1 addition & 1 deletion test/window-integration-tests/test.js
Original file line number Diff line number Diff line change
@@ -66,7 +66,7 @@ describe("getActiveWindow", () => {
it("should determine correct coordinates for our application after moving the window", async () => {
// GIVEN
const xPosition = 42;
const yPosition = 23;
const yPosition = 25;
await app.browserWindow.setPosition(xPosition, yPosition);
await sleep(1000);