How to get the screen size in millimeters using C#

三世轮回 提交于 2019-12-11 13:17:39

问题


Now I'm developing the program which could draw ECG waveform, the speed of ECG waveform is 25 mm/second. So I needs to convert pixels to millimeters.

Now, I wants to get the screen size in millimeters using C#. Now I have 2 monitors, and I hope to know the screen sizes of all monitors, I know how to get the sizes in pixels, but I don't know how to get millimeters value.

I search the google,found that using WMI could get the screen size, I tried, but failed. Could anyone give me some suggestions?


回答1:


I found some code in stackoverflow to get the size of screen in millimeters. Use WMI to enumerate screens, and get device ids. Use device IDs to get EDID from register table. Then read item 21, 22 of "edid" arrary to get width and height of screen, in millimeters.

    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;
    }



回答2:


Refer the following links,

http://www.dallinjones.com/2008/07/how-to-convert-from-pixels-to-millimeters/

Convert Pixels to Inches and vice versa in C#



来源:https://stackoverflow.com/questions/16848779/how-to-get-the-screen-size-in-millimeters-using-c-sharp

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!