Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why do .NET winforms scale improperly at large DPI settings?

Tags:

.net

winforms

My .NET application (VB.NET 3.5 if you really must know) forms do not properly format when rendered at high DPI settings. All of the fixes I've found so far simply explain the cause of the problem as "certain UI elements do not scale properly". I was wondering if anyone had a more meaningful explanation? Thanks!

like image 719
Alex Avatar asked Mar 15 '10 04:03

Alex


People also ask

What is DPI C#?

The dpi value indicates how many pixels there are in a inch (or ppi - pixels per inch).

What is high DPI?

High DPI displays have increased pixel density, compared to standard DPI displays. Pixel density is measured in Dots per Inch (DPI) or Pixels per Inch (PPI), and is determined by the number of display pixels and their size.


1 Answers

Basically the problem is that GDI+ (and by extension, Windows Forms) uses device pixels as its default measurement unit. When you change the DPI setting, the physical size of a pixel doesn't actually change (a pixel is a pixel) but fonts and images that are measured in non-pixel sizes are scaled accordingly. Windows Forms tries to accomodate this as best it can by making more room for layout, but the only true solution is to use a non-pixel based measurement system throughout. This is what WPF does.

In WPF, at 96 DPI, a device-independent pixel corresponds to the same size as a physical pixel. But when you change the DPI of the system, WPF scales the virtual coordinate system globally. The application, the controls, etc all have no idea that they are not aligning to physical pixels anymore. In fact this can sometimes cause aliasing problems in WPF but you pretty much never have to worry about the layout of the application being different in a different DPI setting.

One thing you can do to minimize the problem in Windows Forms is to use the "new" (as of .NET 2.0) layout panels such as TableLayoutPanel, FlowLayoutPanel, etc. Use Anchor, Dock, and AutoSize whenever possible. This will allow Windows Forms to reflow your UI as best it possibly can.

Also beware that if you create forms at a non-standard DPI, Windows Forms will use this as the "base" size. It almost always screws things up. So try to use 96 DPI to design your forms.

like image 62
Josh Avatar answered Oct 07 '22 00:10

Josh