|
| 1 | +# DPI Awareness for DXGI Desktop Duplication |
| 2 | + |
| 3 | +When using DXGI Desktop Duplication (`MonitorCaptureMethod.DesktopDuplication`) for monitor capture on Windows, your application must be configured as **per-monitor DPI aware**. |
| 4 | + |
| 5 | +## The Problem |
| 6 | + |
| 7 | +The DXGI `IDXGIOutput5::DuplicateOutput1` API requires the calling thread to be per-monitor DPI aware. Without this, you will encounter the error: |
| 8 | + |
| 9 | +``` |
| 10 | +IDXGIOutput5::DuplicateOutput1: The calling thread must be per-monitor DPI aware to use the output duplication APIs. |
| 11 | +``` |
| 12 | + |
| 13 | +## Solution |
| 14 | + |
| 15 | +Add an application manifest to your project that declares per-monitor DPI awareness. |
| 16 | + |
| 17 | +### Step 1: Create app.manifest |
| 18 | + |
| 19 | +Create a file named `app.manifest` in your project root: |
| 20 | + |
| 21 | +```xml |
| 22 | +<?xml version="1.0" encoding="utf-8"?> |
| 23 | +<assembly manifestVersion="1.0" xmlns="urn:schemas-microsoft-com:asm.v1"> |
| 24 | + <assemblyIdentity version="1.0.0.0" name="MyApplication.app"/> |
| 25 | + <trustInfo xmlns="urn:schemas-microsoft-com:asm.v2"> |
| 26 | + <security> |
| 27 | + <requestedPrivileges xmlns="urn:schemas-microsoft-com:asm.v3"> |
| 28 | + <requestedExecutionLevel level="asInvoker" uiAccess="false" /> |
| 29 | + </requestedPrivileges> |
| 30 | + </security> |
| 31 | + </trustInfo> |
| 32 | + |
| 33 | + <application xmlns="urn:schemas-microsoft-com:asm.v3"> |
| 34 | + <windowsSettings> |
| 35 | + <!-- Per-monitor DPI awareness V2 - required for DXGI output duplication --> |
| 36 | + <dpiAwareness xmlns="http://schemas.microsoft.com/SMI/2016/WindowsSettings">PerMonitorV2</dpiAwareness> |
| 37 | + <dpiAware xmlns="http://schemas.microsoft.com/SMI/2005/WindowsSettings">true/pm</dpiAware> |
| 38 | + </windowsSettings> |
| 39 | + </application> |
| 40 | + |
| 41 | +</assembly> |
| 42 | +``` |
| 43 | + |
| 44 | +### Step 2: Reference the manifest in your .csproj |
| 45 | + |
| 46 | +Add the `ApplicationManifest` property to your project file: |
| 47 | + |
| 48 | +```xml |
| 49 | +<PropertyGroup> |
| 50 | + <ApplicationManifest>app.manifest</ApplicationManifest> |
| 51 | +</PropertyGroup> |
| 52 | +``` |
| 53 | + |
| 54 | +## Alternative: Use Windows Graphics Capture |
| 55 | + |
| 56 | +If you cannot configure DPI awareness (e.g., library constraints), use Windows Graphics Capture instead: |
| 57 | + |
| 58 | +```csharp |
| 59 | +var source = MonitorCapture.FromPrimary() |
| 60 | + .SetCaptureMethod(MonitorCaptureMethod.WindowsGraphicsCapture); |
| 61 | +``` |
| 62 | + |
| 63 | +Windows Graphics Capture (WGC) is the recommended capture method for Windows 10 1903+ and does not have the DPI awareness requirement. |
| 64 | + |
| 65 | +## More Information |
| 66 | + |
| 67 | +- [Microsoft Docs: High DPI Desktop Application Development](https://docs.microsoft.com/en-us/windows/win32/hidpi/high-dpi-desktop-application-development-on-windows) |
| 68 | +- [Microsoft Docs: DPI Awareness Context](https://docs.microsoft.com/en-us/windows/win32/hidpi/dpi-awareness-context) |
0 commit comments