Disable DPI awareness for WPF application
Asked Answered
R

9

31

Good day!

I've been working on a WPF app for some time now (as a learning experience and oh boy it was a learning experience) and it's finally ready for release. Release means installing it on my HTPC where it will be used to browse my movie collection.

I designed it on my PC which runs 1920*1080 but at the normal DPI setting, while the HTPC/TV is running at the same resolution but a higher DPI setting for obvious reasons.

The problem is my app goes bonkers on the HTPC, messing up pretty much everything as far as visuals go. I know this is due to bad design (mea culpa), but since it's an application that will only be used by me I'm looking for an quick fix, not a full redesign. I read it would be possible to stop the app from being DPI aware by adding the following to AssemblyInfo.cs:

[assembly: System.Windows.Media.DisableDpiAwareness]

However, it doesn't seem to have any effect and the behavior of the app remains the same.

Could anyone point me in the right direction?

Thank you, Johan

Rosebud answered 13/12, 2012 at 11:15 Comment(0)
M
37

DPIAwareness

Just some ideas (not tried):

Are you running on XP? That option might not work on that platform.

What follows are probably just different ways to set the same DpiAwareness option:

  • look at the "compatibility mode" settings on your EXE...right click it's properties...and turn on "Disable display scaling"

  • create a manifest, and say you aren't aware

    <assembly xmlns="urn:schemas-microsoft-com:asm.v1" manifestVersion="1.0" xmlns:asmv3="urn:schemas-microsoft-com:asm.v3" >
     ...
      <asmv3:application>
        <asmv3:windowsSettings xmlns="http://schemas.microsoft.com/SMI/2005/WindowsSettings">
          <dpiAware>false</dpiAware>
        </asmv3:windowsSettings>
      </asmv3:application>
     ...
    </assembly>
    
  • call SetProcessDPIAware (think you have to call this early i.e. before Window is created)

    http://msdn.microsoft.com/en-gb/library/windows/desktop/ms633543(v=vs.85).aspx

You can call IsProcessDPIAware to check if your apps process has had the setting applied to it or not.

Finding out the DPI

Here's how you find out what DPI you are currently using:

Access CompositionTarget via the PresentationSource of your Window to find out what DPI scaling it's using.....you can then use the values to do some scaling adjustments i.e. scale down your "stuff" (whose sizes/length/etc are specified in Device Independent Units), so that when its scaled up due to a higher DPI being in effect it doesn't explode the physical pixel usage (...this can be done in various ways e.g. ViewBox, or calculations on widths, etc on elements ).

double dpiX, double dpiY;
PresentationSource presentationsource = PresentationSource.FromVisual(mywindow);

if (presentationsource != null) // make sure it's connected
{
    dpiX = 96.0 * presentationsource.CompositionTarget.TransformToDevice.M11;
    dpiY = 96.0 * presentationsource.CompositionTarget.TransformToDevice.M22;
}

Do Scaling Adjustments

  • use the ViewBox trick

    See this answer I made before that allowed a Canvas to use positions that were interpreted as "native" pixel no matter what the DPI scaling.

    WPF for LCD screen Full HD

  • access the TransFormToDevice scaling matrix on the CompositionTarget, and from that calculate a new matrix that just undoes that scaling and use that in LayoutTransform or RenderTransform.

  • use a method (maybe even put it in a Converter) that modifies a DIP (Device Independent Position) position/length on an explicit basis....you might do that if you want your Window Size to match a particular pixel size.

Mcgough answered 13/12, 2012 at 11:36 Comment(1)
Both machines run on Windows 7. I've tried the app manifest option, that didn't seem to work. I'll investigate the other options asap, thank you for your input.Rosebud
A
11

This blog article explains how to use a Decorator subclass to do just that.

In short, create a class like this:

namespace MyNamespace
{
    public class DpiDecorator : Decorator
    {
        public DpiDecorator()
        {
            this.Loaded += (s, e) =>
            {
                Matrix m = PresentationSource.FromVisual(this).CompositionTarget.TransformToDevice;
                ScaleTransform dpiTransform = new ScaleTransform(1 / m.M11, 1 / m.M22);
                if (dpiTransform.CanFreeze)
                    dpiTransform.Freeze();
                this.LayoutTransform = dpiTransform;
            };
        }
    };
};

Then add something like xmlns:custom="clr-namespace:MyNamespace" to your top-level window definition, then enclose your DPI-independent user-interface with <custom:DpiDecorator>...</custom:DpiDecorator>.

Anabatic answered 2/3, 2016 at 1:12 Comment(0)
B
4

None of the above truly disable the dpi scaling in WPF.

This is how dpi scaling is calculated in .net 4.6 by WPF: 1) HwndTarget which is the CompositionTarget used by all visuals. 2) UIElement which is the base class for visuals and the place where dpi scaling calculation results are stored.

WPF makes sure the global scaling is calculated once when first visual is prepared. This is controlled by a boolean on HwndTarget {private static bool _setDpi = true}. After calculating the dpi, the _setDpi field is set to false and dpi aware methods are shortcut using code like this {if (!HwndTarget._setDpi) return;}

A similar thing happens for every UIElement, which has the same pattern using {private static bool _setDpi = true} to allow calculation first time.

Next check comes from ProcessDpiAwareness which can be None, Process, or Monitor. In order to stop WPF from considering individual monitors you needs to set ProcessDpiAwareness to Process (int 1).

Finally when dpi is calculated the result is stored in 2 lists called DpiScaleXValues and DpiScaleYValues on UIElement. So you need to initialize these to correct values.

Here is a sample code I use for myself (only works for .net 4.6):

        var setDpiHwnd = typeof(HwndTarget).GetField("_setDpi", BindingFlags.Static | BindingFlags.NonPublic);
        setDpiHwnd?.SetValue(null, false);

        var setProcessDpiAwareness = typeof(HwndTarget).GetProperty("ProcessDpiAwareness", BindingFlags.Static | BindingFlags.NonPublic);
        setProcessDpiAwareness?.SetValue(null, 1, null);

        var setDpi = typeof(UIElement).GetField("_setDpi", BindingFlags.Static | BindingFlags.NonPublic);

        setDpi?.SetValue(null, false);

        var setDpiXValues = (List<double>)typeof(UIElement).GetField("DpiScaleXValues", BindingFlags.Static | BindingFlags.NonPublic)?.GetValue(null);

        setDpiXValues?.Insert(0, 1);

        var setDpiYValues = (List<double>)typeof(UIElement).GetField("DpiScaleYValues", BindingFlags.Static | BindingFlags.NonPublic)?.GetValue(null);

        setDpiYValues?.Insert(0, 1);
Burmaburman answered 25/5, 2017 at 2:46 Comment(2)
This is very appealing, allowing you to get the true 1:1 pixel ratio. However, it is kinda scary to think about all the other things that are already initialized that this does not fix. I encountered one case at SystemParameters.VirtualScreenWidth. This is what I wanted when I searched up this question, but I feel like I can't use it. I wish Microsoft provided a supported way to set the base scaling factor.Brood
This worked perfectly until new Windows 10 update 1903, the application now always uses per monitor scaling. Any ideas what do to with it?Opposite
T
3

A quick fix would be just to wrap your Window's contents in a Viewbox. If the Viewbox's immediate child has an explicit Width and Height, and if the aspect ratio is the same on both machines, then this should get you up and running pretty much immediately.

Translatable answered 13/12, 2012 at 13:16 Comment(1)
Thank you for your input! I tried this, but for some reason my PC runs out of memory! At first it hangs for a long time when I add the ViewBox tags in XAML and I see the memory in use by VS shoot up to above 4 Gb. When I try to compile I run out of memory (on a 12 Gb machine) so something is really wrong, most likely with the things I do within the ViewBox.Rosebud
L
2

DpiAwareness only affects DPI Virtualization. In Windows it shows up as "Use Windows XP style scaling" in the Windows 7 & 8 custom resolution box, where checked means disabled and unchecked means enabled. (Remember to apply on the main font size screen if you change it! OK'ing the box is not enough.)

If you have DPI Virtualization turned off, then it makes zero difference what your app tells the OS it supports, it will always use standard scaling. When it's on, it makes the difference between true scaling (aware) and just slapping a bilinear resize on the whole app (unaware).

I just remembered another caveat, DPI virt won't work at all without Aero Glass working.

Labiate answered 28/2, 2013 at 2:52 Comment(0)
G
1

Calin Pirtea's answer truly disable the DPI scaling.

For those who wants to stretch the application for a display scale more than 100% and accept blurry, Carlos Borau's answer works. Here's is a way for .NET 4.6 (C#):

1 Add a manifest file to your application project

  1. right-click project, Add->New Item
  2. Choose General->Application Manifest File

2 Uncomment following and set dpiAware = false:

  <application xmlns="urn:schemas-microsoft-com:asm.v3">
      <windowsSettings>
          <dpiAware xmlns="http://schemas.microsoft.com/SMI/2005/WindowsSettings">false</dpiAware>
      </windowsSettings>
  </application>
Glossitis answered 28/8, 2018 at 3:17 Comment(0)
E
0

For those using VB.NET, the way to access the app.manifest is:

enter image description here

and then uncomment this part and set it to "false"

enter image description here

Excel answered 10/10, 2017 at 11:41 Comment(0)
D
0

After updating to .NET 4.8 runtime on Windows 10, I was getting the following exception with Calin's approach for setting ProcessDpiAwareness

System.ArgumentException: 'Object of type 'System.Int32' cannot be converted to type 'System.Nullable`1[MS.Win32.NativeMethods+PROCESS_DPI_AWARENESS]'.'

Changing code to below resolves the Exception, but I am not sure if it achieves the same result.

var processDpiAwareness = typeof(HwndTarget).GetProperty("ProcessDpiAwareness", BindingFlags.Static | BindingFlags.NonPublic);

if (processDpiAwareness != null)
{
      var underlyingType = Nullable.GetUnderlyingType(processDpiAwareness.PropertyType);

      if (underlyingType != null)
      {
           var val = Enum.Parse(underlyingType, "PROCESS_SYSTEM_DPI_AWARE");
           processDpiAwareness.SetValue(null, val, null);
      }
 }
Dearr answered 22/8, 2019 at 0:39 Comment(1)
The syntax is correct, but none of the values (PROCESS_DPI_UNAWARE, PROCESS_SYSTEM_DPI_AWARE, and PROCESS_PER_MONITOR_DPI_WAARE) can reach the goal in my Win11+.NET 6. If anyone got lucky, please let me known.Bjork
C
0

In my case, changing high DPI settings of my exe helped. Example below. I choose 'Application' in combobox.

enter image description here

Read more about it here

Chaddie answered 9/2, 2022 at 13:44 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.