如何使用C#获得以毫米为单位的屏幕尺寸 [英] How to get the screen size in millimeters using C#

查看:284
本文介绍了如何使用C#获得以毫米为单位的屏幕尺寸的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

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.

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

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.

我搜索了谷歌,发现使用WMI可以得到屏幕尺寸,我试过了,但是失败了.有人可以给我一些建议吗?

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

推荐答案

我在stackoverflow中找到了一些代码来获取屏幕尺寸(以毫米为单位).使用WMI枚举屏幕,并获取设备ID.使用设备ID从寄存器表中获取EDID.然后阅读急速"操作的第21、22项,以获取屏幕的宽度和高度(以毫米为单位).

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

这篇关于如何使用C#获得以毫米为单位的屏幕尺寸的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆