如何使用C#获得以毫米为单位的屏幕尺寸

时间:2013-05-31 02:11:57

标签: c# size screen

现在我正在开发可绘制心电图波形的程序,心电图波形的速度为25毫米/秒。所以我需要将像素转换为毫米。

现在,我希望使用C#获得以毫米为单位的屏幕尺寸。现在我有2个显示器,我希望知道所有显示器的屏幕尺寸,我知道如何获得像素的大小,但我不知道如何获得毫米值。

我搜索谷歌,发现使用WMI可以获得屏幕大小,我试过,但失败了。有人能给我一些建议吗?

2 个答案:

答案 0 :(得分:4)

我在stackoverflow中找到了一些代码来获取以毫米为单位的屏幕大小。使用WMI枚举屏幕,并获取设备ID。使用设备ID从寄存器表中获取EDID。然后阅读“edid”arrary的第21,22项,以获得屏幕的宽度和高度,以毫米为单位。

    public static List<PointF> GetDesktopMonitors()
    {
        List<PointF> screenSizeList = new List<PointF>();

        //////////////////////////////////////////////////////////////////////////

        try
        {
            ManagementObjectSearcher searcher = new ManagementObjectSearcher("root\\WMI", "SELECT * FROM WmiMonitorID");

            foreach (ManagementObject queryObj in searcher.Get())
            {
                Debug.WriteLine("-----------------------------------------------");
                Debug.WriteLine("WmiMonitorID instance");
                Debug.WriteLine("----------------");
           //   Console.WriteLine("Active: {0}", queryObj["Active"]);
                Debug.WriteLine("InstanceName: {0}", queryObj["InstanceName"]);
           //   dynamic snid = queryObj["SerialNumberID"];
           //   Debug.WriteLine("SerialNumberID: (length) {0}", snid.Length);
                Debug.WriteLine("YearOfManufacture: {0}", queryObj["YearOfManufacture"]);

                /*
                foreach (PropertyData data in queryObj.Properties)
                {
                    Debug.WriteLine(data.Value.ToString());
                }
                */

                dynamic code = queryObj["ProductCodeID"];
                string pcid = "";
                for (int i = 0; i < code.Length; i++)
                {
                    pcid = pcid + Char.ConvertFromUtf32(code[i]);
                  //pcid = pcid +code[i].ToString("X4");
                }
                Debug.WriteLine("ProductCodeID: " + pcid);


                int xSize = 0;
                int ySize = 0;
                string PNP = queryObj["InstanceName"].ToString();
                PNP = PNP.Substring(0, PNP.Length - 2);  // remove _0
                if (PNP != null && PNP.Length > 0) 
                {
                    string displayKey = "SYSTEM\\CurrentControlSet\\Enum\\";
                    string strSubDevice = displayKey + PNP + "\\" + "Device Parameters\\";
                    // e.g.
                    // SYSTEM\CurrentControlSet\Enum\DISPLAY\LEN40A0\4&1144c54c&0&UID67568640\Device Parameters
                    // SYSTEM\CurrentControlSet\Enum\DISPLAY\LGD0335\4&1144c54c&0&12345678&00&02\Device Parameters
                    //
                    Debug.WriteLine("Register Path: " + strSubDevice);

                    RegistryKey regKey = Registry.LocalMachine.OpenSubKey(strSubDevice, false);
                    if (regKey != null)
                    {
                        if (regKey.GetValueKind("edid") == RegistryValueKind.Binary)
                        {
                            Debug.WriteLine("read edid");

                            byte[] edid = (byte[])regKey.GetValue("edid");

                            const int edid_x_size_in_mm = 21;
                            const int edid_y_size_in_mm = 22;
                            xSize = ((int)edid[edid_x_size_in_mm] * 10);
                            ySize = ((int)edid[edid_y_size_in_mm] * 10);
                            Debug.WriteLine("Screen size cx=" + xSize.ToString() + ", cy=" + ySize.ToString());
                        }
                        regKey.Close();
                    }
                }

                Debug.WriteLine("-----------------------------------------------");

                PointF pt = new PointF();
                pt.X = (float)xSize;
                pt.Y = (float)ySize;

                screenSizeList.Add(pt);
            }
        }
        catch (ManagementException e)
        {
            Console.WriteLine("An error occurred while querying for WMI data: " + e.Message);
        }

        return screenSizeList;
    }

答案 1 :(得分:-1)

相关问题