When I run any Windows Forms application in Windows 10, the graphics inside the window appear to be distorted:
At design time this does not happen:
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.
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:
SetProcessDPIAware
API call ExampleYou 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
As it's already mentioned above, it is recommended that you set the process-default DPI awareness via application manifest, not an API call.
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.