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

dg2k 1,386 Reputation points
2022-10-26T07:21:10.68+00:00

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?

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

Accepted answer
  1. Castorix31 81,636 Reputation points
    2022-10-29T07:23:12.807+00:00

    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 5,412 Reputation points Microsoft Employee
    2022-10-27T20:35:49.317+00:00

    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