There are times when you want to get the windows phone device’s screen resolution programmatically from your Windows Phone 8.1 App . In some of my previous blog posts , i explained about the different screen resolutions in windows phone and how the developers can retreive them from their windows phone 7 and windows phone 8 app.
- Q&A #23 – What are the Screen Resolutions Windows Phone 8 Supports?
- How to retrieve the Screen Resolution of Windows Phone 8 using C# ?
- Retrieve the screen resolution of Windows Phone 7 using c#
In this blog post , i will share the code snippet that can be used to retreive the screen resolution of the Windows Phone 8.1 device when using the Windows Runtime (XAML) template.
How to Get screen resolution in Windows Phone 8.1 (WinRT) ?
First retreive the raw pixels per view using the RawPixelsPerViewPixel property defined in the DisplayInformation class . This property is used to retreive the value that represents the number of physical pixels for each view
var rawpixelperview = Windows.Graphics.Display.DisplayInformation.GetForCurrentView().RawPixelsPerViewPixel;
Multiply the value retreived using the RawPixelsPerViewPixel property with the Width and Height of the Window.Current.Bounds. This would get the right resolution of the screen.
var Width = Window.Current.Bounds.Width * rawpixelperview; var Height = Window.Current.Bounds.Height * rawpixelperview;
Below is the complete code snippet used to get the screen resolution from Windows Phone 8.1 device and display it in a MessageDialog.
private void btn1_Click(object sender, RoutedEventArgs e) { var rawpixelperview = Windows.Graphics.Display.DisplayInformation.GetForCurrentView().RawPixelsPerViewPixel; var Width = Window.Current.Bounds.Width * rawpixelperview; var Height = Window.Current.Bounds.Height * rawpixelperview; MessageDialog dialog = new MessageDialog("Resolution = " + Math.Round(Width) + " * " + Math.Round(Height)); dialog.ShowAsync(); }