How to obtain the correct physical size of the monitor?
Asked Answered
F

8

11

How can I get the size of the display in centimeters or inches?

This code does not always works correctly:

HDC hdc = CreateDC(_T("DISPLAY"),dd.DeviceName,NULL,NULL);
int width = GetDeviceCaps(hdc, HORZSIZE);
int height = GetDeviceCaps(hdc, VERTSIZE);
ReleaseDC(0, hdc)

Especially for multi-monitor configuration.

Update: I need to get the size just for ordinary monitors, which have a constant physical size.

Foliose answered 23/2, 2009 at 14:7 Comment(0)
F
20

I found another way. The physical size of the monitor are stored in the EDID, and Windows are almost always copies of its value in the registry. If you can parse EDID, you can read the width and height of the monitor in centimeters.

Update: Added code

BOOL GetMonitorDevice( TCHAR* adapterName, DISPLAY_DEVICE &ddMon )
{
    DWORD devMon = 0;

    while (EnumDisplayDevices(adapterName, devMon, &ddMon, 0))
    {
        if (ddMon.StateFlags & DISPLAY_DEVICE_ACTIVE &&
            ddMon.StateFlags & DISPLAY_DEVICE_ATTACHED) // for ATI, Windows XP
            break;

        devMon++;
    }

    if (ddMon.DeviceString[0] == '\0')
    {
        EnumDisplayDevices(adapterName, 0, &ddMon, 0);
        if (ddMon.DeviceString[0] == '\0')
            _tcscpy_s(ddMon.DeviceString, _T("Default Monitor"));
    }
    return ddMon.DeviceID[0] != '\0';
}

BOOL GetMonitorSizeFromEDID(TCHAR* adapterName, DWORD& Width, DWORD& Height)
{
    DISPLAY_DEVICE ddMon;
    ZeroMemory(&ddMon, sizeof(ddMon));
    ddMon.cb = sizeof(ddMon);

    //read edid
    bool result = false;
    Width = 0;
    Height = 0;
    if (GetMonitorDevice(adapterName, ddMon))
    {
        TCHAR model[8];
        TCHAR* s = _tcschr(ddMon.DeviceID, '\\') + 1;
        size_t len = _tcschr(s, '\\') - s;
        if (len >= _countof(model))
            len = _countof(model) - 1;
        _tcsncpy_s(model, s, len);

        TCHAR *path = _tcschr(ddMon.DeviceID, '\\') + 1;
        TCHAR str[MAX_PATH] = _T("SYSTEM\\CurrentControlSet\\Enum\\DISPLAY\\");
        _tcsncat_s(str, path, _tcschr(path, '\\')-path);
        path = _tcschr(path, '\\') + 1;
        HKEY hKey;
        if(RegOpenKeyEx(HKEY_LOCAL_MACHINE, str, 0, KEY_READ, &hKey) == ERROR_SUCCESS)
        {
            DWORD i = 0;
            DWORD size = MAX_PATH;
            FILETIME ft;
            while(RegEnumKeyEx(hKey, i, str, &size, NULL, NULL, NULL, &ft) == ERROR_SUCCESS)
            {
                HKEY hKey2;
                if(RegOpenKeyEx(hKey, str, 0, KEY_READ, &hKey2) == ERROR_SUCCESS)
                {
                    size = MAX_PATH;
                    if(RegQueryValueEx(hKey2, _T("Driver"), NULL, NULL, (LPBYTE)&str, &size) == ERROR_SUCCESS)
                    {
                        if (_tcscmp(str, path) == 0)
                        {
                            HKEY hKey3;
                            if(RegOpenKeyEx(hKey2, _T("Device Parameters"), 0, KEY_READ, &hKey3) == ERROR_SUCCESS)
                            {
                                BYTE EDID[256];
                                size = 256;
                                if(RegQueryValueEx(hKey3, _T("EDID"), NULL, NULL, (LPBYTE)&EDID, &size) == ERROR_SUCCESS)
                                {
                                    DWORD p = 8;
                                    TCHAR model2[9];

                                    char byte1 = EDID[p];
                                    char byte2 = EDID[p+1];
                                    model2[0]=((byte1 & 0x7C) >> 2) + 64;
                                    model2[1]=((byte1 & 3) << 3) + ((byte2 & 0xE0) >> 5) + 64;
                                    model2[2]=(byte2 & 0x1F) + 64;
                                    _stprintf(model2 + 3, _T("%X%X%X%X"), (EDID[p+3] & 0xf0) >> 4, EDID[p+3] & 0xf, (EDID[p+2] & 0xf0) >> 4, EDID[p+2] & 0x0f);
                                    if (_tcscmp(model, model2) == 0)
                                    {
                                        Width = EDID[22];
                                        Height = EDID[21];
                                        result = true;
                                    }
                                    else
                                    {
                                        // EDID incorrect
                                    }
                                }
                                RegCloseKey(hKey3);
                            }
                        }
                    }
                    RegCloseKey(hKey2);
                }
                i++;
            }
            RegCloseKey(hKey);
        }
    }

    return result;
}
Foliose answered 23/2, 2009 at 21:31 Comment(11)
I've always been curious about the communications channel between a monitor and the graphics interface - thanks for giving the name!Hubbard
A bit more indentation would be nice. That'd make sure I couldn't see the full lines on my 30' computer screen.Subsidy
@devo yea, it's just way too deeply nested. I'd recommend refactoring this code if you plan on using itVicious
Also, it is good to note that not all monitors provide EDID information.Vicious
This registry path is probably device specific, and anyway undocumented. There is a documented alternative - I've put it as an answer below.Scaremonger
You need to do something reasonable if the EDID isn't there. (It's not on my machine.) You also have to take into account things like projectors, where the size of the image depends on the throw distance, zoom setting, etc.Excogitate
The code ddMon.DeviceID != '\0'; compares a TCHAR pointer to '\0'. Perhaps you forgot a reference star?Dextrocular
@KingDragon: I believe some credit is in place. ofekshilon.com/2014/06/19/reading-specific-monitor-dimensionsScaremonger
Code sample appears to have swapped width and height.Excogitate
I have not tested to run this code, but I don't think it would work on my computer. It would just return bytes 22 and 21, which doesn't give my screen dimensions. What does work is the link provided by ofekshilon.com (see comment above) which uses bytes 66, 67 and 68, and gives dimensions in millimeters. On my screen I correctly get w=480mm and h=270mm.Gaulin
I think byte 21 and 22, give display size in cm... that is approximate, but perhaps good enough. However, for some reason Adobe, Gimp, Visio, Word seem to be atuned to some other value with zoom being 100%. The point would be to fix the metrics these programs use. On my setup I get screen content about 7% too large.Epicontinental
S
9

Navigating the registry directly is not only unsupported, but actually fails for devices different than yours. (e.g., the one on which I tested your code).

Unlike what some here say, there is an official way of accessing the EDID key path: by use of the Setup API, and specifically SetupDiOpenDevRegKey.

There's some tedious setup involved - Sample code is here.


EDIT: multiple monitors are handled here.

Scaremonger answered 26/11, 2011 at 21:21 Comment(2)
Your code does not work for multiple monitors since GetsizeForDevID does ignore the used device it always retrieves the size of the last one. Size of first is always ignored.Yseulte
Warning: EDID is not available for all monitors.Excogitate
Y
7

It is not possible to determine the exact physical size of a video device on windows as this depends on quite a lot of variables (e.g. active monitor profile, horizontal/vertical resolution, pixel size, etc.), some of which are not under the control of the computer.

Think for example of projector devices, where the physical size depends on the distance to the projection area which cannot be determined programmatically, as the video projector could be moved manually anytime.

Yuriyuria answered 23/2, 2009 at 14:21 Comment(7)
+1, You'd have to wonder what use it would be to know the physical size rather than the actual resolution.Friesian
/me thinks Kosi2801 just outed himself as being German, by using the word "beamer". It really doesn't mean "video projector" in English. :)Eisenstark
hehe, now I know the slang German for projector. Nice! Stackoverflow as a technical cross cultural melting pot - awesome.Chuckle
Sixlettervariables, you'd want to know the physical size whenever you want to display something, such as a page of text, at "actual size." Maybe in a print-preview scenario.Conquistador
Corrected my english mistakes. Thanks for the hints.Yuriyuria
How exactly could horizontal/vertical resolution and pixel size affect the physical size of a monitor? I've changed resolution a lot, and I've never noticed my monitor growing or shrinking. True about projectors, but that doesn't automatically imply that getting the physical size is always impossible. Just that it's impossible for projectors and the like.Thesaurus
On a CRT monitor, changing resolutions and pixel size can affect how much of the tube is used to display the image, effectively changing the size of the display. CRT images tend to shrink slightly with the age of the tube, which is why many display schemes overscan.Excogitate
C
5

You can't get the real exact size - you can get an approximation that depends on the DPI setting in windows, and the resolution of the screen, but you can't guarantee that this is the real size.

Especially in a multimonitor situation with different displays (say a 19" CRT and 24" LCD). Further, if the display is CRT then the measurement is the tube measurement, and not the display area.

When programs have needed this information exactly in the past, they've shown a gauge onscreen, and had the user hold a piece of paper up to the screen and measure the paper width with the gauge. Given the paper is 8.5" or A4 then you know the width, and you can use the number that they input to figure out the real DPI for a given display. You may need to have them do that for each monitor in a multimonitor setup.

-Adam

Chuckle answered 23/2, 2009 at 14:57 Comment(0)
F
4

Windows Vista and upper support new function GetMonitorDisplayAreaSize() http://msdn.microsoft.com/en-us/library/ms775210%28VS.85%29.aspx

Update: It doesn't work properly

Foliose answered 30/4, 2010 at 21:52 Comment(0)
A
1

You can request LOGPIXELSX from GetDeviceCaps to get the DPI for the display, though it will generally return 96. See also this MSDN article on writing DPI-aware apps.

Atworth answered 23/2, 2009 at 14:51 Comment(0)
A
0

You can obtain EDID from the registry.

Artemus answered 4/10, 2010 at 18:41 Comment(0)
M
0

I recently encountered the same issue and finally found a solution myself

  1. Enumerate all displays with QueryDisplayConfig
if (QueryDisplayConfig(
    QDC_ONLY_ACTIVE_PATHS,
    &pathCount,
    paths,
    &modeCount,
    modes,
    NULL) == ERROR_SUCCESS)
{
    // Use paths and modes
}

paths and modes contain all active display paths, which contain width / height (in pixel), refresh rate, etc.

  1. Query EDID registry path of the display path with DisplayConfigGetDeviceInfo
DISPLAYCONFIG_TARGET_DEVICE_NAME targetName = {
    .header = {
        .type = DISPLAYCONFIG_DEVICE_INFO_GET_TARGET_NAME,
        .size = sizeof(targetName),
        .adapterId = path->targetInfo.adapterId,
        .id = path->targetInfo.id,
    },
};
if(DisplayConfigGetDeviceInfo(&targetName.header) == ERROR_SUCCESS)
{
    // Use targetName.monitorDevicePath
}

In my laptop, targetName.monitorDevicePath is \\?\DISPLAY#PRL5000#3&21de92e8&0&UID0#{e6f07b5f-ee97-4a90-b076-33f57bf4eaa7}. Notibly DISPLAY#PRL5000#3&21de92e8&0&UID0

  1. Replace all # with \; pretend SYSTEM\CurrentControlSet\Enum; append Device Parameters
wchar_t regPath[256] = L"SYSTEM\\CurrentControlSet\\Enum";
wchar_t* pRegPath = regPath + strlen("SYSTEM\\CurrentControlSet\\Enum");
wchar_t* pDevPath = targetName.monitorDevicePath + strlen("\\\\?");
while (*pDevPath && *pDevPath != L'{')
{
    if (*pDevPath == L'#')
        *pRegPath = L'\\';
    else
        *pRegPath = *pDevPath;
    ++pRegPath;
    ++pDevPath;
    assert(pRegPath < regPath + sizeof(regPath) / sizeof(wchar_t) + strlen("Device Parameters"));
}
wcscpy(pRegPath, L"Device Parameters");

You get L"SYSTEM\CurrentControlSet\Enum\DISPLAY\PRL5000\3&21de92e8&0&UID0\Device Parameters"

  1. Read the registry value
uint8_t edidData[1024];
DWORD edidLength = sizeof(edidData);
if (RegGetValueW(HKEY_LOCAL_MACHINE, regPath, L"EDID", RRF_RT_REG_BINARY, NULL, edidData, &edidLength) == ERROR_SUCCESS &&
    edidLength > 0 && edidLength % 128 == 0)
{
    // Read physical size from EDID data
}

I use this function to read physical size from EDID

void ffEdidGetPhysicalSize(const uint8_t edid[128], uint32_t* width, uint32_t* height)
{
    *width = (((uint32_t) edid[68] & 0xF0) << 4) + edid[66];
    *height = (((uint32_t) edid[68] & 0x0F) << 8) + edid[67];
}

For multi-monitor machine, you may match an HMONITOR to an DISPLAYCONFIG_PATH_INFO:

  1. Query source name of the display path with DisplayConfigGetDeviceInfo
DISPLAYCONFIG_SOURCE_DEVICE_NAME sourceName = {
    .header = {
        .type = DISPLAYCONFIG_DEVICE_INFO_GET_SOURCE_NAME,
        .size = sizeof(sourceName),
        .adapterId = path->sourceInfo.adapterId,
        .id = path->sourceInfo.id,
    },
};
if (DisplayConfigGetDeviceInfo(&sourceName.header) == ERROR_SUCCESS)
{
    // Use sourceName.viewGdiDeviceName
}
  1. Get szDevice from a HMONITOR
MONITORINFOEXW monitorInfo;
GetMonitorInfoW(hMonitor, (MONITORINFO*) &monitorInfo);
// Use monitorInfo.szDevice
  1. sourceName.viewGdiDeviceName should contain the same string as monitorInfo.szDevice, which is \\.\DISPLAY1 in my laptop

Full code, which detects current resolution in pixel, refresh rate, DPI scaled resolution in pixel, rotation in degree, monitor brand name, physical size in mm, etc: https://github.com/fastfetch-cli/fastfetch/blob/dev/src/detection/displayserver/displayserver_windows.c

Mulcahy answered 12/7 at 1:56 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.