This article mainly solves two problems
- C# Winform high DPI font blur.
- Under high DPI (scaling > 100%), the UI designer continuously prompts to scale to 100%. If you don't restart at 100%, the designed controls will fly around.


Create a test program
- Create a new .NET Windows Forms App (WinForms) project

- Select .NET 6.0

- Set the form size to 1000 x 1000 for later verification of correct scaling

- Add a button with size 150 x 50

- Add a PictureBox with size 300 x 300, right-click to import an image


- Add test code
namespace WinFormsApp1
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void Form1_Load(object sender, EventArgs e)
{
Text = this.Width + "x" + this.Height + " pic "+ pictureBox1.Width + "x" + pictureBox1.Height + 启动环境();
}
public static string 启动环境()
{
#if NET461
return (".NET Framework 4.6.1");
#elif NET6_0
return (".NET6");
#endif
}
}
}
- Run and see the effect: under .NET 6, the dimensions are all correct

Let's officially begin
- Right-click the project, add an application manifest
app.manifest, keep the filename default, then modify it

Uncomment this section to enable DPI awareness
<application xmlns="urn:schemas-microsoft-com:asm.v3">
<windowsSettings>
<dpiAware xmlns="http://schemas.microsoft.com/SMI/2005/WindowsSettings">true</dpiAware>
<longPathAware xmlns="http://schemas.microsoft.com/SMI/2016/WindowsSettings">true</longPathAware>
</windowsSettings>
</application>
- Double-click the project name to edit the configuration file
Change TargetFrameworks to a dual-target framework: <TargetFrameworks>net6.0-windows;net461;</TargetFrameworks>. After saving, you'll be prompted to reload the project. It's best to close and reopen Visual Studio.
The complete file is as follows
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<OutputType>WinExe</OutputType>
<TargetFrameworks>net6.0-windows;net461;</TargetFrameworks>
<UseWindowsForms>true</UseWindowsForms>
<ApplicationManifest>app.manifest</ApplicationManifest>
<ApplicationVisualStyles>true</ApplicationVisualStyles>
<ApplicationUseCompatibleTextRendering>false</ApplicationUseCompatibleTextRendering>
<ApplicationHighDpiMode>SystemAware</ApplicationHighDpiMode>
</PropertyGroup>
<ItemGroup>
<Compile Update="Properties\Resources.Designer.cs">
<DesignTime>True</DesignTime>
<AutoGen>True</AutoGen>
<DependentUpon>Resources.resx</DependentUpon>
</Compile>
</ItemGroup>
<ItemGroup>
<EmbeddedResource Update="Properties\Resources.resx">
<Generator>ResXFileCodeGenerator</Generator>
<LastGenOutput>Resources.Designer.cs</LastGenOutput>
</EmbeddedResource>
</ItemGroup>
</Project>
- If you are prompted that controls cannot be found, add the following lines to
Form1.Designer.csandForm1.cs
using System;
using System.Windows.Forms;
In
Program.cs, comment outApplicationConfiguration.Initialize();Run and select
net461
Note: My screen is 2800 x 1800 with 175% scaling

As expected, the displayed dimensions are incorrect

- Add
AutoScaleMode = AutoScaleMode.Dpi;to Form1.cs
public Form1()
{
AutoScaleMode = AutoScaleMode.Dpi; // Add this line, above 'InitializeComponent();'
InitializeComponent();
}
Run again

Perfect!
- Double-click the form editor – no prompt about 100% scaling. Add a standard menu and DataGridView for testing

Perfect! Double the pleasure!
Summary
- Create a new .NET Windows Forms App (WinForms) project [.NET 6.0]
- Add an application manifest
app.manifestand enable DPI awareness - Change
TargetFrameworksto dual-target:<TargetFrameworks>net6.0-windows;net461;</TargetFrameworks> - In
Program.cs, comment outApplicationConfiguration.Initialize(); - Add
AutoScaleMode = AutoScaleMode.Dpi;aboveInitializeComponent();
For older projects, you can also upgrade to this new project format by editing the project file, which supports the features described in this article. If you need further tutorials, please leave a comment. I've been on vacation for the past few days, so I'll stop here for today. See you next time!
Accompanying DEMO