How to get screen dpi ?

hi folks!

I need to get the dpi value of the screen, but the screen class does not allow me to access this property. Searching the web was also not successful, just got a note that this can be read out by a declare. Anyway, since I’m not an expert here, I totally hang.

Can you provide me with some help on how to reading the screen dpi on both platforms I need, OS X and Windows?

Would be a great for m project. Thank you,
Carsten.

With 2016R1 and over, if you set your app to be HiDPI aware, you can use Graphics.ScaleX and Graphics.ScaleY.

http://documentation.xojo.com/index.php/graphics

Thank you, but I need the dpi value as integer to do some mathematical calculations. So scaling a graphic is unfortunately useless for what I need to do.

Whatever. Sorry for trying to help. Good luck :stuck_out_tongue:

The base dpi is 72 on OS X and 96 on Windows. If you multiply those by TrueWindow.ScaleFactor, you’ll get what you need.

@Greg O’Lone: Is this always the case regardless what kind of display, graphics card and resolution is being used ?

@Michel: No matter, I appreciate any help, but it was simply not what I looked for. Why are you so impolite? Was not meant offending from my side.

Maybe I should be more precise in what I’m trying to achieve: My goal is to measure elements on the screen in millimeters. For this task I need to know how many pixels are exactly one millimeter.

I thought the best way to do so is to get the dpi value and use it for further calculation. If this is not the right way to do so, please let me know.

Carsten,

I think it would be best to be more precise when asking for help

Remember everyone who helps here does so because they want to and not for being paid

I understand it can be hard to explain upfront what the request is but please be kind and update your question when asked and not expect anything other then community help :slight_smile:

The number of pixels per physical measurement (millimeter, inch) or whatever are NOT going to be consistent, how can they be?

I have a 19" monitor the is 1280 x 1024 pixels, I also have a 27 inch monitor that is 2560 x 1440

the 19" monitor is 14.5 inches in the horizontal direction (remember is 19" diagonal) or 88 pixels per inch
the 27" monitor is 23 inches in the horizontal direction… this is 2560/23 or 111 pixels per inch

my 13" MacBook is 1280x800 and is 11.25" horizontal, making it 113 pixels per inch

DPI does not apply to display devices… since you can have different size monitors with the same resoultion, and the same size monitor with different resolutions.

the 19" screen was 11.36 inches for 1000 pixels
the 27" screen was 9.0 inches for 1000 pixels
the 13" screen was 8.8 inches for 1000 pixels

your screen size WILL vary

[quote=263410:@Dave S]
your screen size WILL vary[/quote]
Isn’t that what Greg addresed by using the dpi AND the TrueScreenSize?

Are you trying to do this to 1:1 scale?

I tried something similar in the past, but because of the variation of screen resolutions (hardware and software), I resorted to producing a scale, which the user could calibrate.

Getting the DPI from the software is fine, but getting the hardware is impossible.

Actually not fair statement; just because I didn’t know how doesn’t make it impossible. Just means I didn’t know how!

You might be able to use system profiler and get the names of the displays, they may include the diagonal inch dimension. You can figure out the aspect ratio and dpi from the software provided screen dimensions.

It may not be precise, but it would be interesting to see.

No… TrueWindow.ScaleFactor will be 1 for “normal” monitors and 2 for Retina… has nothing to do with the resolution other than points vs pixels…

And even if you allow the user to enter a scaling factor to calibrate your control. What if they have multiple montiors? and what if (as mine are), they are different resolutions and sizes? Just playing the devils advocate here, so you don’t create something that works on YOUR computer but won’t work properly anywhere else.

Dave, I find the whole DPI/PPI/Resolution/HiDPI thing highly confusing and I’m sure I’m wrong but isn’t it so that if you know the PPI of a screen (like the OP wants to figure out), you could display a picture that would measure 1 inch if you put a ruler in front of it?

So if I create an image and save it as 8888 & 88 PPI and display it on your 19"
and a 111 * 111 @ 111PPI on your 27"
and a 113
113 @ 113PPI on your 13"
…wouldn’t they all measure 1 inch?

I am sure is not as simple as I think, but I know is possible because we have some in-house CAD software that does exactly that. I will honestly say, however, that I do not know how it works. And based on Dave’s comments (since he has worked on this sort of things before), I will venture to say is not as simple as I first thought.

I have this VB6 code from somewhere on the Internet. Sorry, don’t know how to convert it to Xojo code. Just tested it on 2 Windows 10 computers - a MacMini with a Philips 24inch 1920x1080 monitor and a Sony Viao with a 13inch 1920x1080 display.

First test with the Windows display settings at 100%: code shows 96DPI.
Second test at 125%: code shows same 96DPI, even though things on the screen are drawn much larger that at 100%.

My conclusion, if the code is correct then querying Windows (at least from Vista to Win10) for DPI is not reliable.

[code]Option Explicit

Private Declare Function GetDeviceCaps Lib “gdi32” (ByVal hDc As Long, ByVal nIndex As Long) As Long
Private Declare Function GetDC Lib “user32.dll” (ByVal hwnd As Long) As Long
Private Declare Function ReleaseDC Lib “user32” (ByVal hwnd As Long, ByVal hDc As Long) As Long

Private Const LOGPIXELSX = 88 ’ Logical pixels/inch in X
Private Const LOGPIXELSY = 90 ’ Logical pixels/inch in Y

Private Sub Form_Load()
Dim hDc As Long
Dim lngRetVal As Long
Dim varPixToInchX As Variant
Dim varPixToInchY As Variant

hDc = GetDC(0)

varPixToInchX = GetDeviceCaps(hDc, LOGPIXELSX)
varPixToInchY = GetDeviceCaps(hDc, LOGPIXELSY)

MsgBox "Horz: " & varPixToInchX & vbCrLf & "Vert: " & varPixToInchY

lngRetVal = ReleaseDC(0, hDc)

End Sub
[/code]

This article has a lot of useful information, if you are interested then do go to all the links.
http://www.coffeecup.com/forums/website-design-and-development/72-or-96-dpi/?post_id=103366

Also an explanation from Microsoft:
https://blogs.msdn.microsoft.com/fontblog/2005/11/08/where-does-96-dpi-come-from-in-windows/

Sometimes, you are unable to see yourself when you are inside of you. It is often good to step out of yourself to take a look back. Then you can see yourself.

Knowing what Michel has done all his entire computing life, I will not doubt what he says.

Perhaps you could have phrase your first reply in a not so impolite way?

[quote=263418:@Marco Hof]Dave, I find the whole DPI/PPI/Resolution/HiDPI thing highly confusing and I’m sure I’m wrong but isn’t it so that if you know the PPI of a screen (like the OP wants to figure out), you could display a picture that would measure 1 inch if you put a ruler in front of it?

So if I create an image and save it as 8888 & 88 PPI and display it on your 19"
and a 111 * 111 @ 111PPI on your 27"
and a 113
113 @ 113PPI on your 13"
…wouldn’t they all measure 1 inch?[/quote]

yes, yes they would. but what you DON’T know is the PHYSICAL size of the screen, all you know is the RESOLUTION of the screen

So you know the screen is 1280 pixels wide, but what you don’t know… is it 17", 19" or 21" inches wide?
and even if you include the physical size in your “calibration”… it only works for THAT monitor, and some of us have multiple monitors of diffrent physcial sizes and resolutions.

Right. But it looks that it should be possible for the OP to find the physical size in mm using CGDisplayScreenSize and then do the math.
Well… I think. :slight_smile: