DiscreteTom / rusty-duplication

Capture the screen on Windows using the Desktop Duplication API in Rust, with shared memory support.
MIT License
9 stars 1 forks source link

Wrong screen size #5

Closed DiscreteTom closed 1 year ago

DiscreteTom commented 1 year ago

Sometimes the screen size from DXGI_OUTPUT_DESC is the size after zoomed.

E.g., I have a screen with 2400x1600 and 150% scale, sometimes the returned dimension is 1600x1067.

DiscreteTom commented 1 year ago

Currently the screen size is calcualted from DXGI_OUTPUT_DESC.DesktopCoordinates, which is suitable to locate the screen and scale the screen, but the resolution is wrong in high DPI situation.

IDXGIOutput::GetDesc method (dxgi.h): On a high DPI desktop, GetDesc returns the visualized screen size unless the app is marked high DPI aware. For info about writing DPI-aware Win32 apps, see High DPI.

DiscreteTom commented 1 year ago

https://learn.microsoft.com/en-us/windows/win32/api/shellscalingapi/

GetDpiForMonitor

SetProcessDpiAwareness

DiscreteTom commented 1 year ago

According to ChatGPT:

  1. Get raw dpi through GetDpiForMonitor.
  2. pixelWidth = width * dpi / 96.

E.g. screen with 2400 pixel width, 150% scale, got width 1600 and dpi 144, 2400 = 1600 * 144 / 96.

DiscreteTom commented 1 year ago

From ChatGPT:

Use OutputDuplication->GetDesc(&duplDesc), then the duplDesc.ModeDesc.Width/Height is the actual pixel dimension.