How do you get display Pixels Per Inch (PPI) for MAUI/WinUI?

dg2k 1,161 Reputation points

I used to get display info under Xamarin/UWP with the Windows.Graphics.Display.DisplayInformation API:

var displayInfo = DisplayInformation.GetForCurrentView();

which provides various properties such as ScreenWidthInRawPixels, ScreenHeightInRawPixels, RawPixelsPerViewPixel, RawDpiX, and RawDpiY.

While the new MAUI Display API, DeviceDisplay.Current.MainDisplayInfo (Microsoft.Maui.Devices namespace), provides most of the UWP equivalent properties, the PPI properties RawDpiX and RawDpiY are missing.

Is there any way to get PPI info under WinUI?

A Microsoft open-source framework for building native device applications spanning mobile, tablet, and desktop.
1,424 questions
0 comments No comments
{count} votes

Accepted answer
  1. Castorix31 71,626 Reputation points

    Is there any way to get PPI info under WinUI?

    There are several ways.
    I often use GetDpiForWindow or GetDpiForSystem
    (I used GetDpiForWindow in some samples I had uploaded, like : CMediaEngine.cs)

1 additional answer

Sort by: Most helpful
  1. Rob Caplan - MSFT 4,862 Reputation points Microsoft Employee

    You can get a UIElement.XamlRoot from the WinUI UIElement backing a MAUI control and then get the mapping of raw pixels to view pixels from XamlRoot.RasterizationScale and do the math from there to get what you need.

    If that isn't sufficient, you can p-invoke to Win32 API, but it's a few more steps to get the native HWND.

    Future versions of WinUI will include more direct access to this info via DisplayInformation

    0 comments No comments