Get Screen Resolution in Win10 Uwp App

Get Screen Resolution in Win10 UWP App

To improve the other answers even a bit more, the following code also takes care of scaling factors, e.g. for my 200% for my Windows display (correctly returns 3200x1800) and 300% of the Lumia 930 (1920x1080).

var bounds = ApplicationView.GetForCurrentView().VisibleBounds;
var scaleFactor = DisplayInformation.GetForCurrentView().RawPixelsPerViewPixel;
var size = new Size(bounds.Width*scaleFactor, bounds.Height*scaleFactor);

As stated in the other answers, this only returns the correct size on Desktop before the size of root frame is changed.

How to obtain Windows Screen Resolution in UWP (XAML) App

ScreenHeightInRawPixels and ScreenWidthInRawPixels property should be able to work on Surface. However please note they are newly added in Windows 10 Anniversary Update (aka RS1 Update).

To use these two properties please make sure your project targets Windows 10 Anniversary Edition or later. You can check the Target Version in Properties like in the following screenshot.

Sample Image

Besides, you may also need to check the OS version of your Surface and make sure the OS Build is 14393 or later. You can find the OS Build in "Setting""System""About".

Sample Image

Maximize / Resize to Display Resolution a Windows-10 C# UWP App?

Ultimately, I choose to maximize my UWP App (written in C#) in my workspace, that area of my screen above my Windows taskbar. I would like to provide minimal instructions for creating a maximized app that you know is working.

I created a new default Blank App (Universal Windows) using C# in Visual Studio Community 2019, called "Draw Bounding Boxes". I included spaces here so that I could access "Draw Bounding Boxes" with spaces from my Start Menu.

I replaced the contents of "App.xaml.cs" with the following code block.

namespace Draw_Bounding_Boxes
{
/// <summary>
/// Provides application-specific behavior to supplement the default Application class.
/// </summary>
sealed partial class App : Windows.UI.Xaml.Application
{
/// <summary>
/// Invoked when the application is launched normally by the end user. Other entry points
/// will be used such as when the application is launched to open a specific file.
/// </summary>
/// <param name="e">Details about the launch request and process.</param>
protected override void OnLaunched(Windows.ApplicationModel.Activation.LaunchActivatedEventArgs e)
{
// Resize app.
uint screenWidthInRawPixels = Windows.Graphics.Display.DisplayInformation.GetForCurrentView().ScreenWidthInRawPixels;
uint screenHeightInRawPixels = Windows.Graphics.Display.DisplayInformation.GetForCurrentView().ScreenHeightInRawPixels;
double rawPixelsPerViewPixel = Windows.Graphics.Display.DisplayInformation.GetForCurrentView().RawPixelsPerViewPixel;
double screenWidthInViewPixels = System.Convert.ToDouble(screenWidthInRawPixels) / rawPixelsPerViewPixel;
double screenHeightInViewPixels = System.Convert.ToDouble(screenHeightInRawPixels) / rawPixelsPerViewPixel;

// If offsetToScreenWidthInViewPixels is less than 15,
// on first load app will be of default size, and on second load app will be full screen.
// A loaded image will have height equal to full screen height minus app title bar height minus app toolbar height minus 5 view pixels of padding.
// Part of a loaded image with aspect ratio less than one will be behind Windows taskbar.
// This is all very complicated and undesirable.
// If offsetToScreenHeightInViewPixels is less than 40,
// on first load app will be of default size, and on second load app will be full screen.
// A loaded image will have height equal to full screen height minus app title bar height minus app toolbar height minus 5 view pixels of padding.
// Part of a loaded image with aspect ratio less than one will be behind Windows taskbar.
// This is all very complicated and undesirable.
// If offsetToScreenWidthInViewPixels is greater than or equal to 15 and offsetToScreenHeightInViewPixels is greater than or equal to 40,
// on first load app will be of PreferredLaunchViewSize, and a loaded image with aspect ratio less than one will have height exactly equal to height of app minus app title bar height minus app toolbar height.
// If PreferredLaunchViewSize.Height is only screenHeightInViewPixels - offsetToScreenHeightInViewPixels,
// part of app and a loaded image with aspect ratio less than one will be behind taskbar.
// If taskbarHeight is taken off of screenHeightInViewPixels - offsetToScreenHeightInViewPixels,
// bottom of app and coincident bottom of loaded image will be slightly above taskbar.
// I consider this ideal.
double offsetToScreenWidthInViewPixels = 15;
double offsetToScreenHeightInViewPixels = 40;
double taskbarHeight = 40;
Windows.UI.ViewManagement.ApplicationView.PreferredLaunchViewSize = new Windows.Foundation.Size(screenWidthInViewPixels - offsetToScreenWidthInViewPixels, screenHeightInViewPixels - offsetToScreenHeightInViewPixels - taskbarHeight);
Windows.UI.ViewManagement.ApplicationView.PreferredLaunchWindowingMode = Windows.UI.ViewManagement.ApplicationViewWindowingMode.PreferredLaunchViewSize;

// Set the app window to a new Frame.
Windows.UI.Xaml.Controls.Frame rootFrame = new Windows.UI.Xaml.Controls.Frame();
Windows.UI.Xaml.Window.Current.Content = rootFrame;

// Navigate the frame to the initial default page.
rootFrame.Navigate(typeof(MainPage), e.Arguments);

// Attempts to activate the application window by bringing it to the foreground and setting the input focus to it.
Windows.UI.Xaml.Window.Current.Activate();

} // protected override void OnLaunched
} // sealed partial class App
} // namespace Draw_Bounding_Boxes

I added property x:Name="page" to the <Page> tag in "MainPage.xaml".

I removed the <Grid> </Grid> environment from MainPage.xaml.

I replaced the contents of "MainPage.xaml.cs" with the following code block.

// Create namespace Draw_Bounding_Boxes to contain all classes associated with our app.
namespace Draw_Bounding_Boxes
{
// Create class MainPage that inherits fields and methods from Windows.UI.Xaml.Controls.Page and
// is used to declare and define user-interface elements and functionality.
public sealed partial class MainPage : Windows.UI.Xaml.Controls.Page
{
// Create constructor public MainPage.
public MainPage()
{
// Necessary to instantiate this Page, add a stackPanel to this Page, et cetera.
this.InitializeComponent();

// Find width of app in view pixels and height between bottom of app and bottom of title bar in view pixels.
double widthOfAppInViewPixels = Windows.UI.ViewManagement.ApplicationView.PreferredLaunchViewSize.Width;
double heightBetweenBottomOfAppAndBottomOfTitleBarInViewPixels = Windows.UI.ViewManagement.ApplicationView.PreferredLaunchViewSize.Height;

// Create a stackPanel.
Windows.UI.Xaml.Controls.StackPanel stackPanel = new Windows.UI.Xaml.Controls.StackPanel();

// Create a toolbar with width equal to the width of the app, height equal to 50 view pixels, and background color of light blue that has one row and four columns.
Windows.UI.Xaml.Controls.Grid toolbar = new Windows.UI.Xaml.Controls.Grid();
toolbar.Width = widthOfAppInViewPixels;
toolbar.Height = 50;
toolbar.Background = new Windows.UI.Xaml.Media.SolidColorBrush(Windows.UI.Colors.AliceBlue);
Windows.UI.Xaml.Controls.RowDefinition row = new Windows.UI.Xaml.Controls.RowDefinition();
toolbar.RowDefinitions.Add(row);
Windows.UI.Xaml.Controls.ColumnDefinition column = new Windows.UI.Xaml.Controls.ColumnDefinition();
column.Width = new Windows.UI.Xaml.GridLength(widthOfAppInViewPixels);
toolbar.ColumnDefinitions.Add(column);

stackPanel.Children.Add(toolbar);

page.Content = stackPanel;
}
}
}

Universal Windows 10 App JS Resolution setting

Actually I solved my issue by disabling scaling like what is said in this page.
The JS

trySetDisableLayoutScaling(true)

DID NOT Work. but the css media-query scaling worked.

@media (max-height: 1440px) {   
@-ms-viewport {
height: 1440px;
}
}

In one of my apps I did have to set width too like

  @media (max-height: 1440px) {   
@-ms-viewport {
height: 1440px;
width:1440px;
}
}

UWP : Bounds doesn't return actual Screen Resolution

In order to get the actual resolution, you need to call ApplicationView.GetForCurrentView().VisibleBoundsbefore window is really displayed. The better place is in App.xaml.cs just after the Window.Current.Activate() method.
This post explains the start up window size very clearly.

Below is my test code:

In App.xaml.cs:
//here set preferred size = 800*800 for test
ApplicationView.PreferredLaunchWindowingMode = ApplicationViewWindowingMode.PreferredLaunchViewSize;
ApplicationView.PreferredLaunchViewSize = new Size(800, 800);

Window.Current.Activate();
//here to get the real full screen size
var bounds = ApplicationView.GetForCurrentView().VisibleBounds;
var full = ApplicationView.GetForCurrentView().IsFullScreen;
var scaleFactor = DisplayInformation.GetForCurrentView().RawPixelsPerViewPixel;
var size = new Size(bounds.Width * scaleFactor, bounds.Height * scaleFactor);

in my Mainpage.xaml.cs:
//here the size should be 800 * 800
private void Button_Click(object sender, RoutedEventArgs e)
{
var bounds = ApplicationView.GetForCurrentView().VisibleBounds;
var full = ApplicationView.GetForCurrentView().IsFullScreen;
var scaleFactor = DisplayInformation.GetForCurrentView().RawPixelsPerViewPixel;
var size = new Size(bounds.Width * scaleFactor, bounds.Height * scaleFactor);
}

Besides, as about your question, how to handle the desktop and tablet mode, could you clarify which situation you want to handle? If you want to adapt the UI layout based on different screen resolution, you can refer to MSDN online help about adaptive UI.

Set UWP app display resolution

You can't set the screen resolution from the app - there's no config or API for that. You'll need to scale the app up. (Or maybe try using a XAML webview that's set to 1280x720 and sits in a viewbox which scales it up.)



Related Topics



Leave a reply



Submit