private DisplayInformationEx(Size physicalSize, Size physicalResolution, DisplayInformationSource informationSource) { PhysicalSize = physicalSize; PhysicalDiagonal = PhysicalSize.GetHypotenuse(); PhysicalResolution = physicalResolution; AspectRatio = physicalSize.Height / physicalSize.Width; if (!(AspectRatio.IsCloseEnoughTo(physicalResolution.Height / physicalResolution.Width))) { throw new ArgumentOutOfRangeException("physicalResolution", "only square pixels supported"); } RawDpi = physicalResolution.Width / physicalSize.Width; AbsoluteScaleFactorBeforeNormalizing = PhysicalSize.Width / DisplayConstants.BaselineWidthInInches; RawPixelsPerViewPixel = GenerateRawPixelsPerViewPixel(); ViewResolution = new Size(PhysicalResolution.Width / RawPixelsPerViewPixel, PhysicalResolution.Height / RawPixelsPerViewPixel); ViewPixelsPerInch = RawDpi / RawPixelsPerViewPixel; // Adjust for different aspect ratio, if necessary ViewPixelsPerHostPixel = Math.Min(ViewResolution.Width / Application.Current.Host.Content.ActualWidth, ViewResolution.Height / Application.Current.Host.Content.ActualHeight); HostPixelsPerViewPixel = 1 / ViewPixelsPerHostPixel; InformationSource = informationSource; }
private DeviceDisplayInfo(Size physicalSize, Size physicalResolution, DisplayInformationSource displayInformationSource) { PhysicalSize = physicalSize; InformationSource = displayInformationSource; PhysicalDiagonal = PhysicalSize.GetHypotenuse(); PhysicalResolution = physicalResolution; AspectRatio = physicalSize.Height / physicalSize.Width; if (!(AspectRatio.IsCloseEnoughTo(physicalResolution.Height / physicalResolution.Width))) { throw new ArgumentOutOfRangeException("physicalResolution", "only square pixels supported"); } RawDpi = physicalResolution.Width / physicalSize.Width; double DiagonalToWidthRatio15To9 = 9.0 / Math.Sqrt(Math.Pow(15, 2) + Math.Pow(9, 2)); // var AbsoluteScaleFactorBeforeNormalizing = PhysicalSize.Width / BaselineWidthInInches; // Calculate Scaling factor, Never less than 1 or more view pixels than physical pixels var scale = Math.Max(1, AbsoluteScaleFactorBeforeNormalizing); var idealViewWidth = Math.Min(BaselineWidthInViewPixels * scale, PhysicalResolution.Width); var idealScale = PhysicalResolution.Width / idealViewWidth; RawPixelsPerViewPixel = idealScale.NudgeToClosestPoint(1); ViewResolution = new Size(PhysicalResolution.Width / RawPixelsPerViewPixel, PhysicalResolution.Height / RawPixelsPerViewPixel); ViewPixelsPerInch = RawDpi / RawPixelsPerViewPixel; // Adjust for different aspect ratio, if necessary ViewPixelsPerHostPixel = Math.Min(ViewResolution.Width / Application.Current.Host.Content.ActualWidth, ViewResolution.Height / Application.Current.Host.Content.ActualHeight); HostPixelsPerViewPixel = 1 / ViewPixelsPerHostPixel; }