MrZweistein
MrZweistein

Reputation: 112

Is there a better or more elegant method to find out the effective pixel dimensions of the actual display?

I am currently developing several UWP apps where I want to make use of the information of the effective Pixel dimensions of the display/screen the application is running in. Is there a better way to retrieve that information than what I did (as far I can judge it works correctly so far)?

enum DimensionType { Height, Width }
public static class DisplayInfo
{
    public static uint EffictivePixelHeight
    {
        get { return GetEffectivePixels(DimensionType.Height); }
    }

    public static uint EffectivePixelWidth
    {
        get { return GetEffectivePixels(DimensionType.Width); }
    }

    /// <summary>
    /// Calculate Effective Pixels based on RawPixelCount and ResolutionScale
    /// </summary>
    /// <param name="t">calculate width or height</param>
    /// <returns>0 if invalid, effecective pixel height/width based on t</returns>
    private static uint GetEffectivePixels(DimensionType t)
    {
        DisplayInformation info = DisplayInformation.GetForCurrentView();
        uint r = 0;
        switch (t)
        {
            case DimensionType.Height:
                r = info.ScreenHeightInRawPixels;
                break;
            case DimensionType.Width:
                r = info.ScreenWidthInRawPixels;
                break;
            default:
                break;
        }
        float sf = 0;
        switch (info.ResolutionScale)
        {
            case ResolutionScale.Invalid:
                sf = 0;
                break;
            case ResolutionScale.Scale100Percent:
                sf = 1;
                break;
            case ResolutionScale.Scale120Percent:
                sf = 1 / 1.2f;
                break;
            case ResolutionScale.Scale125Percent:
                sf = 1 / 1.25f;
                break;
            case ResolutionScale.Scale140Percent:
                sf = 1 / 1.4f;
                break;
            case ResolutionScale.Scale150Percent:
                sf = 1 / 1.5f;
                break;
            case ResolutionScale.Scale160Percent:
                sf = 1 / 1.6f;
                break;
            case ResolutionScale.Scale175Percent:
                sf = 1 / 1.75f;
                break;
            case ResolutionScale.Scale180Percent:
                sf = 1 / 1.8f;
                break;
            case ResolutionScale.Scale200Percent:
                sf = 1 / 2f;
                break;
            case ResolutionScale.Scale225Percent:
                sf = 1 / 2.25f;
                break;
            case ResolutionScale.Scale250Percent:
                sf = 1 / 2.5f;
                break;
            case ResolutionScale.Scale300Percent:
                sf = 1 / 3f;
                break;
            case ResolutionScale.Scale350Percent:
                sf = 1 / 3.5f;
                break;
            case ResolutionScale.Scale400Percent:
                sf = 1 / 4f;
                break;
            case ResolutionScale.Scale450Percent:
                sf = 1 / 4.5f;
                break;
            case ResolutionScale.Scale500Percent:
                sf = 1 / 5f;
                break;
            default:
                sf = 0;
                break;
        }

        r = (uint)(r * sf);

        return r;
    }
}

Upvotes: 1

Views: 383

Answers (1)

Sunteen Wu
Sunteen Wu

Reputation: 10627

It seems like that the way you are trying to get the effective Pixel dimensions of the display/screen is right. But you are using an amount of switch branches to calculate the scale factor which looks very tedious and unnecessary. Actually you may only need one code line to calculate the scale factor.

double scaleFactor = DisplayInformation.GetForCurrentView().RawPixelsPerViewPixel; 

So after simplify your code snippet may just as follows:

enum DimensionType { Height, Width }
public static class DisplayInfo
{
   public static uint EffictivePixelHeight
   {
       get { return GetEffectivePixels(DimensionType.Height); }
   }

   public static uint EffectivePixelWidth
   {
       get { return GetEffectivePixels(DimensionType.Width); }
   }

   /// <summary>
   /// Calculate Effective Pixels based on RawPixelCount and ResolutionScale
   /// </summary>
   /// <param name="t">calculate width or height</param>
   /// <returns>0 if invalid, effecective pixel height/width based on t</returns>
   private static uint GetEffectivePixels(DimensionType t)
   {
       DisplayInformation info = DisplayInformation.GetForCurrentView();
       uint r = 0;
       switch (t)
       {
           case DimensionType.Height:
               r = info.ScreenHeightInRawPixels;
               break;
           case DimensionType.Width:
               r = info.ScreenWidthInRawPixels;
               break;
           default:
               break;
       } 
       double scaleFactor = info.RawPixelsPerViewPixel;               
       r = (uint)(r * (1 / scaleFactor));  
       return r;
   }
}

More details please reference DisplayInformation class.

Upvotes: 1

Related Questions