Get Screen Resolution in Win10 UWP App

28,912

Solution 1

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.

Solution 2

Call this method anywhere, anytime (tested in mobile/desktop App):

public static Size GetCurrentDisplaySize() {
    var displayInformation = DisplayInformation.GetForCurrentView();
    TypeInfo t = typeof(DisplayInformation).GetTypeInfo();
    var props = t.DeclaredProperties.Where(x => x.Name.StartsWith("Screen") && x.Name.EndsWith("InRawPixels")).ToArray();
    var w = props.Where(x => x.Name.Contains("Width")).First().GetValue(displayInformation);
    var h = props.Where(x => x.Name.Contains("Height")).First().GetValue(displayInformation);
    var size = new Size(System.Convert.ToDouble(w), System.Convert.ToDouble(h));
    switch (displayInformation.CurrentOrientation) {
    case DisplayOrientations.Landscape:
    case DisplayOrientations.LandscapeFlipped:
        size = new Size(Math.Max(size.Width, size.Height), Math.Min(size.Width, size.Height));
        break;
    case DisplayOrientations.Portrait:
    case DisplayOrientations.PortraitFlipped:
        size = new Size(Math.Min(size.Width, size.Height), Math.Max(size.Width, size.Height));
        break;
    }
    return size;
}

Solution 3

The more simple way:

var displayInformation = DisplayInformation.GetForCurrentView();
var screenSize = new Size(displayInformation.ScreenWidthInRawPixels, 
                          displayInformation.ScreenHeightInRawPixels);

This does not depends on current view size. At any time it returns real screen resolution.

Solution 4

Okay so the Answer from Juan Pablo Garcia Coello lead me to the Solution - Thanks for that!

You can use

var bounds = ApplicationView.GetForCurrentView().VisibleBounds;

but you must call it before the windows is displayed in my case right after

Window.Current.Activate();

is a good place. At this time you will get the bounds of the window on which your app will appear.

Thanks a lot for help me solving it :)

Regards Alex

Solution 5

The only way I found is inside the constructor of a Page:

 public MainPage()
    {
        this.InitializeComponent();

        var test = ApplicationView.GetForCurrentView().VisibleBounds;
    }

I have not tested in Windows 10 Mobile, when the new release appears I will test that.

Share:
28,912
Alex Witkowski
Author by

Alex Witkowski

Updated on February 27, 2020

Comments

  • Alex Witkowski
    Alex Witkowski about 4 years

    As an UWP App runs in window mode on common desktop systems the "old" way of getting the screen resolution won't work anymore.

    Old Resolution with Window.Current.Bounds was like shown in.

    Is there another way to get the resolution of the (primary) display?

  • Alex Witkowski
    Alex Witkowski over 8 years
    That seems not to be working. I get 838 width and 900 height on my full HD screen.
  • Alex Witkowski
    Alex Witkowski over 8 years
    After rootFrame.SizeChanged var bounds = ApplicationView.GetForCurrentView().VisibleBounds; does not give me the right resolution. My scale on Desktop is 100 so that doesn't effect.
  • sibbl
    sibbl over 8 years
    Updated my answer a bit. The reason is that the codes of the other answers don't take care of the scalings (200% on my Windows device and 300% for the Lumia 930) and are wrong on my devices.
  • Alex Witkowski
    Alex Witkowski over 8 years
    Okay that's right for sure. You may edit it one more time and mention that it only works before the size of root frame is changed. than your answer will be the complete solution ;)
  • sibbl
    sibbl over 8 years
    Ah I see. Yeah one better adds it after Window.Current.Activate(); or in another part of the code which runs before the root frame size changes (that's why it worked in several ViewModel constructors where I just tried it...).
  • Mahender
    Mahender over 8 years
    Just to improve the answer, you can change bounds.Height to bounds.Bottom which gives exact resolution value in Windows Mobile and Desktop windows. var size = new Size(bounds.Width * scaleFactor, bounds.Bottom * scaleFactor);
  • Alex Witkowski
    Alex Witkowski about 8 years
    The question was how to get the screen resolution. UWP Apps can run in windows mode on Windows 10 Desktop. so Windows size is not exactly what I needed here.
  • Emil
    Emil over 7 years
    @Mahender bounds.Bottom and bounds.Height returns same value for me
  • T.S
    T.S almost 7 years
    FYI, not anywhere anytime: Windows.Graphics.Display: GetForCurrentView must be called on a thread that is associated with a CoreWindow.
  • Justin XL
    Justin XL over 6 years
    Genius. This should be the accepted answer as the current one doesn't handle app used in external monitors.
  • Justin XL
    Justin XL over 6 years
    Unfortunately this answer is no longer valid in the latest Windows since VisibleBounds doesn't return the max window size (in effective pixels) anymore. Please refer to Vyacheslav's answer below.
  • Stef
    Stef over 5 years
    Thank you, this is a much better answer than all the others here.