本博客所有资源,包括技术文章、软件、代码、还有脚本仅供技术学习交流,请勿用于商业及非法用途,如产生法律纠纷与本人无关,请在24小时内请及时删除。

C# 获取显示器物理尺寸

        public virtual List<string> GetMonitorPnpDeviceId() {
            List<string> rt = new List<string>();
            using (ManagementClass mc = new ManagementClass("Win32_DesktopMonitor")) {
                using (ManagementObjectCollection moc = mc.GetInstances()) {
                    foreach (var o in moc) {
                        var each = (ManagementObject) o;
                        object obj = each.Properties["PNPDeviceID"].Value;
                        if (obj == null)
                            continue;

                        rt.Add(each.Properties["PNPDeviceID"].Value.ToString());
                    }
                }
            }

            return rt;
        }

        public virtual byte[] GetMonitorEdid(string monitorPnpDevId) {
            return (byte[])Registry.GetValue(@"HKEY_LOCAL_MACHINE\SYSTEM\ControlSet001\Enum\" + monitorPnpDevId + @"\Device Parameters", "EDID", new byte[]{ });
        }

        //获取显示器物理尺寸(cm)
        public virtual SizeF GetMonitorPhysicalSize(string monitorPnpDevId) {
            byte[] edid = GetMonitorEdid(monitorPnpDevId);
            if (edid.Length < 23)
                return SizeF.Empty;

            return new SizeF(edid[21], edid[22]);
        }

        //通过屏显示器理尺寸转换为显示器大小(inch)
        public static float MonitorScaler(SizeF moniPhySize) {
            double mDSize = Math.Sqrt(Math.Pow(moniPhySize.Width, 2) + Math.Pow(moniPhySize.Height, 2)) / 2.54d;
            return (float)Math.Round(mDSize, 1);
        }

调用方法:

            SystemInfo sysInfo = new SystemInfo();
            string id = sysInfo.GetMonitorPnpDeviceId()[0];
            SizeF size = sysInfo.GetMonitorPhysicalSize(id);
            MessageBox.Show(SystemInfo.MonitorScaler(size).ToString() + @"");

 

posted @ 2017-05-10 17:43  INRG  阅读(3345)  评论(1编辑  收藏  举报