Resolution, Scale and DPI
This page is under construction
Most PowerShell screenshot utilities follow a standard practice, which is to get the screen resolution of the VirtualScreen
1
Add-Type -AssemblyName System.Windows.Forms
2
[System.Windows.Forms.SystemInformation]::VirtualScreen
Copied!
When running this command in Windows Terminal, it gives me a Width x Height of 1920 x 1080 (1080p)

Display Scale of High Res Screens

The problem is that my screen is not 1080p, but rather, it is a 4K screen. So why does VirtualScreen only see 1080p? That's because on my laptop, I have the scale set to 200%
So when I use any other PowerShell screenshot utility, the captured image is 1080p instead of 4K. This makes my full screen Windows Settings look like this
So in my case, I need to multiply the VirtualScreen 1920 x 1080 by 200% to get my true resolution of 3840 x 2160 (4K)

PowerShell, ISE, and Windows Terminal

Things get worse because there is no uniformity between the different flavors of PowerShell. Remember Windows Terminal shows 1080p
And Windows PowerShell shows 1080p
But PowerShell ISE shows the real 4K of 3840 x 2160

DPI

The solution is to determine the Scale by pulling the information from the DPI class. In the script below, it results in 200 (percent). This matches the Scale from my Windows Settings
1
Add-Type @'
2
using System;
3
using System.Runtime.InteropServices;
4
using System.Drawing;
5
6
public class DPI {
7
[DllImport("gdi32.dll")]
8
static extern int GetDeviceCaps(IntPtr hdc, int nIndex);
9
10
public enum DeviceCap {
11
VERTRES = 10,
12
DESKTOPVERTRES = 117
13
}
14
15
public static float scaling() {
16
Graphics g = Graphics.FromHwnd(IntPtr.Zero);
17
IntPtr desktop = g.GetHdc();
18
int LogicalScreenHeight = GetDeviceCaps(desktop, (int)DeviceCap.VERTRES);
19
int PhysicalScreenHeight = GetDeviceCaps(desktop, (int)DeviceCap.DESKTOPVERTRES);
20
21
return (float)PhysicalScreenHeight / (float)LogicalScreenHeight;
22
}
23
}
24
'@ -ReferencedAssemblies 'System.Drawing.dll' -ErrorAction Stop
25
Return [DPI]::scaling() * 100
Copied!
Last modified 8mo ago