WPF Application same size at every system scale (scale independent) WPF Application same size at every system scale (scale independent) wpf wpf

WPF Application same size at every system scale (scale independent)


Finally found an answer. First get system DPI scale using one of the options below:

  • Read from registry AppliedDPI dword located in Computer\HKEY_CURRENT_USER\Control Panel\Desktop\WindowMetrics. Then divide it by 96.
  • Or use this snippet:

    double dpiFactor = System.Windows.PresentationSource.FromVisual(this).CompositionTarget.TransformToDevice.M11;

    that returns a value between 1.0 to 2.5

Then create a config file that holds application settings and set dpiFactor as default scale. If user preferred a custom scale, call this function on window startup:

private void UserInterfaceCustomScale(double customScale){    // Change scale of window content    MainContainer.LayoutTransform = new ScaleTransform(customScale, customScale, 0, 0);    Width *= customScale;    Height *= customScale;    // Bring window center screen    var screenHeight = System.Windows.SystemParameters.PrimaryScreenHeight;    var screenWidth = System.Windows.SystemParameters.PrimaryScreenWidth;    Top  = ( screenHeight - Height ) / 2;    Left = ( screenWidth  - Width )  / 2;}