DPI handling
DPI (Dots Per Inch) is related to monitor resolution and size. High DPI value means smaller pixels and crisper image.
Support for DPI scaling varies between .NET Framework, .NET ‘core’ (.NET 5..9) and Windows versions. Consult Microsoft guidelines for specific details (e.g. https://learn.microsoft.com/en-us/windows/win32/sbscs/application-manifests#dpiAwareness).
UWP supports monitors with different DPI generally well and scales app content quite nicely.
Axes have methods to convert axis values (data point values) to screen coordinates and screen coordinates to axis values. Use ValueToCoord method to convert an axis value to a screen coordinate, and CoordToValue to convert a screen coordinate to an axis value. Set UseDIP = False, if pixels are preferred, not Device independent pixels (DIPs).
General
By default, .NET Framework WPF applications are DPI aware, whereas WinForms apps only DPI aware in newer versions (starting with the .NET Framework 4.7 and with the Windows 10 Creators Update). Also, DIPs (Device Independent Pixel) are used instead of pixels to measure sizes with WPF.
.NET 'core' support for DPI is quite similar to both WPF and WinForms.
.NET Framework
Application may or may not automatically resize when moved to another screen with different DPI settings. To enable application resizing, AllowDPIChangeInduceWindowsResize property under ChartOptions needs to be set true. Alternatively, user can register to OnDPIChanged event and change its allowWindowResize attribute. These have no effect in WinForms.
// Enabling automatic resizing
chart.Options.AllowDPIChangeInduceWindowResize = true;
// Via OnDPIChanged -event
chart.OnDPIChanged += chart_OnDPIChanged;
private void chart_OnDPIChanged(LightningChart chart, float dpix, float dpiy, ref bool allowWindowResize)
{
allowWindowResize = true;
}
WinForms
WinForms DPI support is most limited, but still can adapt to various DPI settings. Application can receive DPI change events (from Form), but controls not.
Application support for DPI can be configured at app.config file (recommended) or at app.manifest file.
Windows Forms introduces a new System.Windows.Forms.ApplicationConfigurationSection element to support new features and customizations added starting with the .NET Framework 4.7. To take advantage of the new features that support high DPI, add the following to your application configuration file.
<System.Windows.Forms.ApplicationConfigurationSection>
<add key="DpiAwareness" value="PerMonitorV2" />
</System.Windows.Forms.ApplicationConfigurationSection>
WPF
WPF has broader DPI support. Controls can also receive DPI change events.
Application support for DPI can be configured at app.config file (recommended) or at app.manifest file.
NET 6+
Generally NET 6+ supports DPI quite well. Default DPI Awareness for NET application is System aware and it can be changed from application project file by ApplicationHighDpiMode property like so:
<PropertyGroup>
<OutputType>WinExe</OutputType>
<TargetFramework>net6.0-windows</TargetFramework>
<UseWindowsForms>true</UseWindowsForms>
<ApplicationHighDpiMode>PerMonitorV2</ApplicationHighDpiMode>
</PropertyGroup>
DpiHelper class
LightningChart has DpiHelper class, which contains helpers on DPI related issues.
DpiAware states if the system process is DPI aware or not. However, it is currently not possible to distinguish between system aware and per-monitor aware.
bool isDPIAware = DpiHelper.DpiAware;
DpiXFactor/ DpiYFactor is an effective Zoom factor of the system DPI of the screen width/height. Factor that describes how many real pixels there are per one DPI in X/Y direction.
float dpiXFactor = DpiHelper.DpiXFactor;
DipToPx and PxToDip methods convert DIPs to pixels and vice versa using system DPI settings. They can convert single points or pixels, or alternatively the size and the position values of a rectangle.
double pixelValue = DpiHelper.DipToPx(dipValue);