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)
This browser is no longer supported.
Upgrade to Microsoft Edge to take advantage of the latest features, security updates, and technical support.
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?
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)
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