33

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?

Community
  • 1
  • 1
Alex Witkowski
  • 545
  • 1
  • 6
  • 13

7 Answers7

61

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.

sibbl
  • 3,203
  • 26
  • 38
  • 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. – Alex Witkowski Aug 11 '15 at 08:49
  • 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. – sibbl Aug 11 '15 at 08:53
  • 1
    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 ;) – Alex Witkowski Aug 11 '15 at 08:56
  • 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...). – sibbl Aug 11 '15 at 09:03
  • 3
    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);` – Mahender Sep 16 '15 at 18:50
  • @Mahender bounds.Bottom and bounds.Height returns same value for me – Emil Jan 15 '17 at 14:44
  • 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. – Justin XL Jan 08 '18 at 12:06
12

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;
}
SeregaLBN
  • 209
  • 3
  • 5
  • FYI, not anywhere anytime: Windows.Graphics.Display: GetForCurrentView must be called on a thread that is associated with a CoreWindow. – T.S Jul 20 '17 at 13:34
  • 1
    Genius. This should be the accepted answer as the current one doesn't handle app used in external monitors. – Justin XL Aug 07 '17 at 00:18
11

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.

Vyacheslav
  • 111
  • 1
  • 2
5

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

Community
  • 1
  • 1
Alex Witkowski
  • 545
  • 1
  • 6
  • 13
2

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.

Juan Pablo Garcia Coello
  • 3,192
  • 1
  • 23
  • 33
1

Use this method to get the screen size:

public static Size GetScreenResolutionInfo() 
{ 
    var applicationView = ApplicationView.GetForCurrentView(); 
    var displayInformation = DisplayInformation.GetForCurrentView(); 
    var bounds = applicationView.VisibleBounds; 
    var scale = displayInformation.RawPixelsPerViewPixel; 
    var size = new Size(bounds.Width * scale, bounds.Height * scale); 
    return size; 
} 

You should call this method from App.xaml.cs, after Window.Current.Activate(); in OnLaunched method.

Here's the sample code and you can download the full project.

Ann Yu
  • 137
  • 1
  • 4
-3

Just set a name for main Grid or Page and call its width or height for an element you want:

Element.Height = PagePane.Height;
Element.width = PagePane.Width;

it's the easiest way you can use!

Aminkhs
  • 45
  • 2
  • 3
    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. – Alex Witkowski Mar 11 '16 at 10:23