6

When I run any Windows Forms application in Windows 10, the graphics inside the window appear to be distorted:

Bad Image

At design time this does not happen:

Good Image

Has anyone ever experienced this?

(Please,open the images to see better.)

CJBS
  • 15,147
  • 6
  • 86
  • 135
Vinicius Gonçalves
  • 2,514
  • 1
  • 29
  • 54
  • 1
    Have you enabled some accessibility or scaling related settings in Windows or the program itself? – cbr Nov 07 '15 at 22:07
  • 2
    I have the same issue with mine, not really an answer, but I think it is to do with the DPI scaling. if you have a High res screen. you can test it by compiling, then run the exe, right click and in compatibility tick the disable display scaling. I ended up using WPF that doesn't suffer from the same thing. – SmithMart Nov 07 '15 at 22:08
  • Hi @cubrr, I just installed the windows, do not change any settings. I just checked if there is any accessibility configuration that could be influencing the problem, but did not find anything related: / – Vinicius Gonçalves Nov 07 '15 at 22:12
  • Hi @SmithMart !!! I did what you told me. The application went back to work as expected. Thank you very much! – Vinicius Gonçalves Nov 07 '15 at 22:15
  • 1
    No probs, if the app is just for you then all good, if you want others to use it then keep in mind it will happen to them if they have a high dpi setup. like 1080 on 22" screen or so. to be fair though, I still have to set that setting on loads of applications! – SmithMart Nov 07 '15 at 22:18
  • I'm trying to find a way to set the executable with this option checked by default. What did you do in your case? – Vinicius Gonçalves Nov 07 '15 at 22:20
  • Do you remember what setting you set when you load the application? – Vinicius Gonçalves Nov 07 '15 at 22:22
  • 1
    I used the posted solution with windows 8.1 that may apply in windows 10 too. – Reza Aghaei Nov 07 '15 at 22:22

1 Answers1

7

Updated answer for .NET Framework >= 4.7

Open app.config and add the following section:

<System.Windows.Forms.ApplicationConfigurationSection>
    <add key="DpiAwareness" value="PerMonitorV2" />
</System.Windows.Forms.ApplicationConfigurationSection>

For more information, see: High DPI support in Windows Forms.

.NET Framework < 4.7

To solve the problem, you can make your application DPI-Aware using either of these options:

Important Note: It is recommended that you set the process-default DPI awareness via application manifest, not an API call.

Using Application Manifest File

To make the application DPI-Aware, you can add an Application Manifest File to your project. Then in the app.manifest file, uncomment the part that is related to DPI-Awareness:

<application xmlns="urn:schemas-microsoft-com:asm.v3">
 <windowsSettings>
   <dpiAware xmlns="http://schemas.microsoft.com/SMI/2005/WindowsSettings">true</dpiAware>
 </windowsSettings>
</application>

Then in your app.config file, add EnableWindowsFormsHighDpiAutoResizing setting its value to true:

<appSettings>
  <add key="EnableWindowsFormsHighDpiAutoResizing" value="true" />
</appSettings>

For more information take a look at the following topic in Microsoft docs:

  • High DPI Desktop Application Development on Windows

SetProcessDPIAware API call Example

You can use SetProcessDPIAware() method before showing your main form to set your application dpi aware and prevent windows from scaling the application. Also you should check the windows version to be greater than or equals to vista:

static class Program
{
    [DllImport("user32.dll", SetLastError = true)]
    static extern bool SetProcessDPIAware();

    /// <summary>
    /// The main entry point for the application.
    /// </summary>
    [STAThread]
    static void Main()
    {
        if (Environment.OSVersion.Version.Major >= 6)
            SetProcessDPIAware();

        Application.EnableVisualStyles();
        Application.SetCompatibleTextRenderingDefault(true);
        Application.Run(new Form1());
    }
}

Notes

  1. As it's already mentioned above, it is recommended that you set the process-default DPI awareness via application manifest, not an API call.

  2. Before using API calls, read the documentations to know about supported OS and also possible race condition if a DLL caches dpi settings during initialization. Also keep in mind, DLLs should accept the dpi setting of the host process rather than API call themselves.

  3. You may find this DpiHelper class implemented in WinForms for .NET Core 3.0 useful.

Reza Aghaei
  • 120,393
  • 18
  • 203
  • 398