For an application which will be used to calibrate optical systems, I need to know the scaling a user has set in his/her settings... I have tried to get the DPI values for the physical screen and the virtual screen (with applied user scaling). By dividing I should get the scaling factor... but I haven't managed to get it to work.
So is there a way to get the scaling factor right away through a DLL or something else? Or was my way correct and I have to calculate it? If so: how can I get the two values for physical screen and the virtual screen (with applied user scaling)?
I have tried to Use the "user32.dll", the "Shcore.dll", Control.AutoScaleFactor
and the System.Drawing
... In any way I only get the values for the virtual screen (with applied user scaling) or a relationship of 1 which would mean the user has set its scaling to 100% which isn't true. I did not manage to get the physical screen DPI or a correct scaling factor
Here's what I have tried by using the GetDpiForMonitor
function
{
[DllImport("Shcore.dll")]
private static extern int GetDpiForMonitor(IntPtr hmonitor, MonitorDpiType dpiType, out uint dpiX, out uint dpiY);
private enum MonitorDpiType
{
EffectiveDpi = 0,
AngularDpi = 1,
RawDpi = 2,
}
private void ShowPPI_Click(object sender, EventArgs e)
{
IntPtr primaryMonitor = IntPtr.Zero;
uint dpiX, dpiY;
int result = GetDpiForMonitor(primaryMonitor, MonitorDpiType.EffectiveDpi, out dpiX, out dpiY);
MessageBox.Show(result.ToString());
}
}