Created
May 14, 2025 21:45
-
-
Save MuntashirAkon/6d822a3201dabb55687177b072a4764d to your computer and use it in GitHub Desktop.
Get DPI scale for the main display in Windows 11 (24H2). This is useful if you have different DPI scale for different displays and configure VcXsrv and GDK to scale windows correctly in WSL2.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add-Type @' | |
using System; | |
using System.Runtime.InteropServices; | |
using System.Drawing; | |
public class DPI { | |
[DllImport("gdi32.dll")] | |
static extern int GetDeviceCaps(IntPtr hdc, int nIndex); | |
public enum DeviceCap { | |
VERTRES = 10, | |
DESKTOPVERTRES = 117 | |
} | |
public static float scaling() { | |
Graphics g = Graphics.FromHwnd(IntPtr.Zero); | |
IntPtr desktop = g.GetHdc(); | |
int LogicalScreenHeight = GetDeviceCaps(desktop, (int)DeviceCap.VERTRES); | |
int PhysicalScreenHeight = GetDeviceCaps(desktop, (int)DeviceCap.DESKTOPVERTRES); | |
return (float)PhysicalScreenHeight / (float)LogicalScreenHeight; | |
} | |
} | |
'@ -ReferencedAssemblies 'System.Drawing.dll' -ErrorAction Stop | |
Write-Output ([DPI]::scaling()) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment