To solve the problem, you can make your application DPI-Aware using either of these options:
Using Application Manifest File
Using SetProcessDPIAware
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:
For more information take a look at High DPI MSDN page.
Using SetProcessDPIAware
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());
}
}
Note
While I previously used the SetProcessDPIAware method to solve the problem, but read the note before use:
SetProcessDPIAware is subject to a possible race condition if a DLL caches dpi settings during initialization. For this reason, it is recommended that dpi-aware be set through the application (.exe) manifest rather than by calling SetProcessDPIAware.
SetProcessDPIAware is available for use only in the
operating systems specified in the Requirements section. This method
may have unexpected behavior in subsequent versions of the operating
system. Use SetProcessDpiAwareness instead.
DLLs should accept the dpi setting of the host process rather than
call SetProcessDPIAware themselves. To be set properly, dpiAware
should be specified as part of the application (.exe) manifest.
To solve the problem, you can make your application DPI-Aware using either of these options:
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:Then in your app.config file, add
EnableWindowsFormsHighDpiAutoResizing
setting its value to true:For more information take a look at High DPI MSDN page.
Using SetProcessDPIAware
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:Note
While I previously used the
SetProcessDPIAware
method to solve the problem, but read the note before use: