I want to convert from points to pixels (C#) based on current machine’s dpi. While doing so, a particular machine (Lenovo Yoga 500) returns 144 dpi in WPF application and 96 dpi in Windows Forms application. This is quite strange to get different values from same machine.
This is my code.
Graphics graphics = Graphics.FromHwnd(IntPtr.Zero);
MessageBox.Show(“DPI : “ + graphics.DpiX.ToString());
Need someone to help me on this to get same value.
This is because WPF apps are , by default, System DPI Aware, and therefore will give you System DPI. WinForms app, on the other hand, are DPI Unaware, and so will always give you 96.0 as DPI.
If you want the exact DPI of your monitor, your best bet is to make your app Per Monitor DPI Aware and use GetDpiForMonitor API in shcore.dll (ShellScalingApi.h).