Search code examples
c#winrt-xamlscalewindows-8.1screen-resolution

Detect screen scaling factor in Windows 8.1 store apps


I am building up a string for the source attribute of some of my images in a Windows 8.1 store app.

I need to detect the scale factor that the device requires so that I can append the correct .scale-n to the end of the string. These image sources are external to the app so I cannot rely on Windows' automatic scaling by just referencing the image without the scale-n part.

So I need to know whether I should append scale-100, scale-140 or scale-180 to the image URL.

How can I work this out in c# and xaml?


Solution

  • I found a solution for this:

    I created this variable:

    ResolutionScale resolutionScale = Windows.Graphics.Display.DisplayInformation.GetForCurrentView().ResolutionScale;
    

    and then I used a switch statement:

    switch (resolutionScale)
    {
      case ResolutionScale.Scale100Percent:
        //Device is 100
        break;
      case ResolutionScale.Scale140Percent:
        //Device is 140
        break;
      case ResolutionScale.Scale180Percent:
        //Device is 180
        break;
    }