Even if you declare full DPI awareness in the application manifest and/or at run-time, some Windows functions return 96 DPI (or System DPI) values for some reason. You have to pinvoke the new API:
public enum SystemMetric:int { SM_CXSMICON = 49 }
[DllImport("user32.dll")]
static extern int GetSystemMetricsForDpi(SystemMetric smIndex, uint dpi);
[DllImport("user32.dll")]
static extern uint GetDpiForWindow(IntPtr hwnd);
HwndSource source = (HwndSource)HwndSource.FromVisual(this);
IntPtr hWnd = source.Handle;
uint dpi = GetDpiForWindow(hWnd);
int width = GetSystemMetricsForDpi(SM_CXSMICON, dpi);
These functions only exist on Windows 10 update 1607 and later. If you need to support older versions you must manually scale the incorrect DPI value yourself (x = x * dpi / 96
) instead of calling these functions.
See also: