---
title: "Handle per-monitor DPI scaling in C#"
url: https://recursiveai.org/oneplatform/cookbook/handle-per-monitor-dpi/
description: "Convert a native window’s physical pixels to logical units in C#. Update your layout when its scale changes and keep drawing at the real pixel size."
version: "1.0.0"
---

# Handle per-monitor DPI scaling in C#

**OnePlatform** is a free, open-source library for windows, input, clipboard, displays and gamepads in .NET from Recursive AI. MIT or Apache-2.0, no native DLLs to ship.

[NuGet](https://www.nuget.org/packages/RecursiveAI.OnePlatform) · [GitHub](https://github.com/recursive-ai-oss/oneplatform-csharp) · [Docs](https://recursiveai.org/oneplatform/docs/) · [Download example (.zip)](https://recursiveai.org/oneplatform/cookbook/handle-per-monitor-dpi/handle-per-monitor-dpi.zip)

Divide physical pixels by the window’s scale to get logical layout units. OnePlatform reports per-monitor DPI on Windows; X11 uses a desktop-wide scale.

```shell
dotnet add package RecursiveAI.OnePlatform
```

Requires [.NET 10](https://dotnet.microsoft.com/download/dotnet/10.0) (net10.0); .NET 9 and earlier aren’t supported.

```csharp
using OnePlatform;

using var app = PlatformApp.Create();
var window = app.CreateWindow(new() { Title = "DPI demo" });
PrintWidth();
while (true)
{
    app.PumpEvents(100);
    while (app.TryGetEvent(out var evt))
    {
        switch (evt.Kind)
        {
            case PlatformEventKind.WindowScaleChanged:
                PrintWidth();
                break;
            case PlatformEventKind.WindowCloseRequested:
            case PlatformEventKind.QuitRequested:
                return;
        }
    }
}

void PrintWidth() => Console.WriteLine(
    FormattableString.Invariant($"{window.Width / window.Scale:0.##}"));
```

[The example](https://recursiveai.org/oneplatform/cookbook/handle-per-monitor-dpi/handle-per-monitor-dpi.zip) is the full program below: unzip it and `dotnet run`. To run only the code above, paste it into Program.cs of a new `dotnet new console` project and `dotnet run`.

This prints the logical width at startup and after a scale change. Close the window to exit. On X11, changing the desktop scale from 100% to 150% printed:

```text
1280
853.33
```

## Full program

```csharp
using OnePlatform;

using var app = PlatformApp.Create();
var window = app.CreateWindow(new() { Title = "DPI demo" });
ReportSize();

while (true)
{
    app.PumpEvents(100);
    while (app.TryGetEvent(out var evt))
    {
        if (evt.Kind is PlatformEventKind.WindowCloseRequested
            or PlatformEventKind.QuitRequested)
            return;
        if (evt.Kind is PlatformEventKind.WindowScaleChanged
            or PlatformEventKind.WindowResized)
            ReportSize();
    }
}

void ReportSize()
{
    var scale = window.Scale;
    var width = window.Width / scale;
    var height = window.Height / scale;
    Console.WriteLine(FormattableString.Invariant(
        $"Scale {scale:0.##}: {width:0.##} × {height:0.##} logical"));
}
```

The download reports the logical width and height on startup and again when the scale or client size changes. This run started at `Xft.dpi: 96`, then changed the X11 desktop setting to `Xft.dpi: 144` while the window was open:

```text
Scale 1: 1280 × 720 logical
Scale 1.5: 853.33 × 480 logical
```

A monitor change only yields a new scale if the backend reports a different value. On X11, moving between monitors alone doesn’t change this desktop-wide setting.

## Variations

For a renderer that needs a backing buffer in physical pixels, keep the original client size. Add this to `ReportSize` in the full program:

```csharp
Console.WriteLine(FormattableString.Invariant(
    $"Buffer: {window.Width} × {window.Height}"));
```

## Related recipes

- [Create a native desktop window in C#](https://recursiveai.org/oneplatform/cookbook/create-native-cross-platform-window/)
- [Simulate mouse and keyboard input in headless tests](https://recursiveai.org/oneplatform/cookbook/test-window-input-headlessly/)

## The short answer

In a OnePlatform window, divide `Width`, `Height`, or a window-local pointer coordinate in pixels by `window.Scale` to get logical units. Use those units for layout and hit testing. On `WindowScaleChanged` or `WindowResized`, read the current window values and update the layout again. Use the physical client size for the renderer’s buffer, so it still has a pixel for each physical pixel in the client area.

## How do you detect when a window’s DPI changes?

Read `window.Scale` when the window opens rather than assuming 100%. Then handle `WindowScaleChanged` in the event queue. The event carries the new scale and the ID of the affected window. If you’re managing several windows, look up the matching window before updating its layout. Listen for `WindowResized` too: the available layout area can change even at the same scale. The full program uses the window’s current size and scale after either event so both updates go through one calculation.

On Windows, OnePlatform’s Win32 backend requests [Per-Monitor V2 DPI awareness](https://learn.microsoft.com/en-us/windows/win32/hidpi/high-dpi-desktop-application-development-on-windows#per-monitor-and-per-monitor-v2-dpi-awareness). It converts the window DPI to a scale by dividing by 96. The same event handler can respond to an X11 settings change, but that’s not a per-monitor event: the X11 backend reads `Xft.dpi` for the desktop. The [platform support table](https://recursiveai.org/oneplatform/docs/overview/#support) lists how each backend gets its scale.

## How do you convert physical pixels to logical pixels at fractional DPI?

Keep the scale fractional. At 150%, one logical unit spans 1.5 physical pixels. A 300-pixel client width is 200 logical units; a pointer 75 physical pixels from the client’s left edge is 50 logical units from that edge. Divide when reading from pixel space; multiply when converting back to pixel space. Keep fractions in your layout, then round only where you need a whole pixel. If you divide the buffer size as well as the layout size, you no longer have a buffer sized to the real client area.
