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

dg2k 1,416 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?

Developer technologies | .NET | .NET MAUI
0 comments No comments
{count} votes

Accepted answer
  1. Castorix31 90,686 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 6,037 Reputation points Microsoft Employee Moderator
    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

Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.