WPF获取屏幕分辨率以及长度的转换 您所在的位置:网站首页 显示器怎么换算英寸 WPF获取屏幕分辨率以及长度的转换

WPF获取屏幕分辨率以及长度的转换

2024-06-30 19:03| 来源: 网络整理| 查看: 265

相关背景:

WPF的坐标单位是以1/96英寸为一个逻辑像素单位。如果将操作系统的DPI设置成实际的物理DPI,则可以做到分辨率与设备无关。

下面的DipHelp类,可以实现厘米与DPI,英寸与DPI的相互转换,以及获取系统分辨率,物理分辨率.。

public static class DipHelper { /// /// Converts millimeters to DIP (Device Independant Pixels). /// /// A millimeter value. /// A DIP value. public static double MmToDip(double mm) { return CmToDip(mm / 10.0); } /// /// Converts centimeters to DIP (Device Independant Pixels). /// /// A centimeter value. /// A DIP value. public static double CmToDip(double cm) { return (cm * 96.0 / 2.54); } /// /// Converts inches to DIP (Device Independant Pixels). /// /// An inch value. /// A DIP value. public static double InchToDip(double inch) { return (inch * 96.0); } /// /// Converts DIP to inches /// /// /// public static double DipToInch(double dip) { return dip / 96D; } /// /// Converts font points to DIP /// /// A font point value. /// A DIP value. public static double PtToDip(double pt) { return (pt * 96.0 / 72.0); } /// /// Converts DIP to centimeters. /// /// A DIP value. /// A centimeter value. public static double DipToCm(double dip) { return (dip * 2.54 / 96.0); } /// /// Converts DIP to millimeters. /// /// A DIP value. /// A millimeter value. public static double DipToMm(double dip) { return DipToCm(dip) * 10.0; } /// /// Gets the system DPI scale factor (compared to 96 dpi). /// From http://blogs.msdn.com/jaimer/archive/2007/03/07/getting-system-dpi-in-wpf-app.aspx /// Should not be called before the Loaded event (else XamlException mat throw) /// /// A Point object containing the X- and Y- scale factor. private static Point GetSystemDpiFactor() { PresentationSource source = PresentationSource.FromVisual(Application.Current.MainWindow); Matrix m = source.CompositionTarget.TransformToDevice; return new Point(m.M11, m.M22); } private const double DpiBase = 96.0; /// /// Gets the system configured DPI. /// /// A Point object containing the X- and Y- DPI. public static Point GetSystemDpi() { Point sysDpiFactor = GetSystemDpiFactor(); return new Point( sysDpiFactor.X * DpiBase, sysDpiFactor.Y * DpiBase); } /// /// Gets the physical pixel density (DPI) of the screen. /// /// Size - in inch - of the diagonal of the screen. /// A Point object containing the X- and Y- DPI. public static Point GetPhysicalDpi(double diagonalScreenSize) { Point sysDpiFactor = GetSystemDpiFactor(); double pixelScreenWidth = SystemParameters.PrimaryScreenWidth * sysDpiFactor.X; double pixelScreenHeight = SystemParameters.PrimaryScreenHeight * sysDpiFactor.Y; double formatRate = pixelScreenWidth / pixelScreenHeight; double inchHeight = diagonalScreenSize / Math.Sqrt(formatRate * formatRate + 1.0); double inchWidth = formatRate * inchHeight; double xDpi = Math.Round(pixelScreenWidth / inchWidth); double yDpi = Math.Round(pixelScreenHeight / inchHeight); return new Point(xDpi, yDpi); } /// /// Converts a DPI into a scale factor (compared to system DPI). /// /// A Point object containing the X- and Y- DPI to convert. /// A Point object containing the X- and Y- scale factor. public static Point DpiToScaleFactor(Point dpi) { Point sysDpi = GetSystemDpi(); return new Point( dpi.X / sysDpi.X, dpi.Y / sysDpi.Y); } /// /// Gets the scale factor to apply to a WPF application /// so that 96 DIP always equals 1 inch on the screen (whatever the system DPI). /// /// Size - in inch - of the diagonal of the screen /// A Point object containing the X- and Y- scale factor. public static Point GetScreenIndependentScaleFactor(double diagonalScreenSize) { return DpiToScaleFactor(GetPhysicalDpi(diagonalScreenSize)); } }

 



【本文地址】

公司简介

联系我们

今日新闻

    推荐新闻

    专题文章
      CopyRight 2018-2019 实验室设备网 版权所有