Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Getting actual screen dpi/ppi under windows

Tags:

I would like to get the actual screen dpi/ppi, not the dpi setting used for font in C++.

I tried with the following codes:

Version 1, reports 72 dpi, which is wrong.

SetProcessDPIAware(); //true HDC screen = GetDC(NULL); double hSize = GetDeviceCaps(screen, HORZSIZE); double vSize = GetDeviceCaps(screen, VERTSIZE); double hRes = GetDeviceCaps(screen, HORZRES); double vRes = GetDeviceCaps(screen, VERTRES); double hPixelsPerInch = hRes / hSize * 25.4; double vPixelsPerInch = vRes / vSize * 25.4; ReleaseDC(NULL, screen); return (hPixelsPerInch + vPixelsPerInch) * 0.5; 

Version 2, reports 96 dpi, which is the Windows dpi setting for font, but not the actual screen dpi.

SetProcessDPIAware(); //true HDC screen = GetDC(NULL); double hPixelsPerInch = GetDeviceCaps(screen,LOGPIXELSX); double vPixelsPerInch = GetDeviceCaps(screen,LOGPIXELSY); ReleaseDC(NULL, screen); return (hPixelsPerInch + vPixelsPerInch) * 0.5; 
like image 545
Andy Li Avatar asked Sep 29 '12 12:09

Andy Li


People also ask

How do I check my DPI on Windows 10?

Double-click Display icon (can also right-click on desktop and select Properties). Select Settings. Select Advanced. Under the General tab, find the DPI setting.

What is the default Windows DPI?

DPI setting controls the size of the text, apps and icons. A lower DPI setting will make them appear smaller and a higher setting will make them appear bigger. By default Windows has setting of 96 DPI.


2 Answers

I'm honestly confused by the answers here.

Microsoft has a GetDpiForMonitor method:

https://msdn.microsoft.com/en-us/library/windows/desktop/dn280510(v=vs.85).aspx

And monitors DO expose their physical dimensions to tools. You can read your monitors width and height, in centimeters, using the HWiNFO64 tool. So if they're getting it (DDI?), it stands to reason that you can access that information yourself.

Even a different Stack Overflow post mentions using WmiMonitorBasicDisplayParams to get the data.

How to get monitor size

So the top post is flat-out, 100%, wrong.

like image 160
Katastic Voyage Avatar answered Sep 21 '22 16:09

Katastic Voyage


What you're asking for is, unfortunately, not possible in the general case.

Windows doesn't know the physical screen size. Windows might know that your screen has 1024x768 pixels, but it doesn't know how big the screen actually is. You might pull the cable out of your old 13" screen and connect it to a 19" monitor without changing the resolution. The DPI would be different, but Windows won't notice that you changed monitors.

You can get the true physical dimensions and DPI for a printer (assuming the driver isn't lying), but not for a screen. At least not reliably.

UPDATED

As others have pointed out, there are standards for two-way communication between newer monitors and the OS (EDID), that might make this information available for some devices. But I haven't yet found a monitor that provides this information.

Even if EDID were universally available, it's still not solvable in the general case, as the display could be a video projector, where the DPI would depend on the zoom, the focus, the lens type, and the throw distance. A projector is extremely unlikely to know the throw distance, so there's no way for it to report the actual DPI.

like image 32
Adrian McCarthy Avatar answered Sep 18 '22 16:09

Adrian McCarthy