Skip to content

Commit 8e08930

Browse files
Capture physical desktop pixels in smoke screenshots
Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
1 parent aac30c6 commit 8e08930

1 file changed

Lines changed: 54 additions & 18 deletions

File tree

testing/release-smoke/Install-AndLaunchRelease.ps1

Lines changed: 54 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -89,6 +89,9 @@ public static class ReleaseSmokeNativeMethods
8989
9090
[DllImport("user32.dll")]
9191
public static extern bool SetProcessDPIAware();
92+
93+
[DllImport("gdi32.dll")]
94+
public static extern int GetDeviceCaps(IntPtr hdc, int nIndex);
9295
}
9396
'@
9497
}
@@ -99,6 +102,44 @@ public static class ReleaseSmokeNativeMethods
99102
Add-Type -AssemblyName System.Drawing
100103
}
101104

105+
function Get-PhysicalDesktopBounds {
106+
Initialize-ScreenshotSupport
107+
108+
$sourceDc = [ReleaseSmokeNativeMethods]::GetDC([IntPtr]::Zero)
109+
if ($sourceDc -eq [IntPtr]::Zero) {
110+
throw 'GetDC failed while reading desktop dimensions.'
111+
}
112+
113+
try {
114+
$desktopHorzRes = 118
115+
$desktopVertRes = 117
116+
$horzRes = 8
117+
$vertRes = 10
118+
119+
$width = [ReleaseSmokeNativeMethods]::GetDeviceCaps($sourceDc, $desktopHorzRes)
120+
$height = [ReleaseSmokeNativeMethods]::GetDeviceCaps($sourceDc, $desktopVertRes)
121+
122+
if ($width -le 0 -or $height -le 0) {
123+
$width = [ReleaseSmokeNativeMethods]::GetDeviceCaps($sourceDc, $horzRes)
124+
$height = [ReleaseSmokeNativeMethods]::GetDeviceCaps($sourceDc, $vertRes)
125+
}
126+
127+
if ($width -le 0 -or $height -le 0) {
128+
throw "Desktop device dimensions are invalid: width=$width, height=$height."
129+
}
130+
131+
[pscustomobject]@{
132+
Left = 0
133+
Top = 0
134+
Width = $width
135+
Height = $height
136+
}
137+
}
138+
finally {
139+
[ReleaseSmokeNativeMethods]::ReleaseDC([IntPtr]::Zero, $sourceDc) | Out-Null
140+
}
141+
}
142+
102143
function Wait-UniGetUIWindow {
103144
param(
104145
[Parameter(Mandatory)]
@@ -135,31 +176,26 @@ function Save-DesktopScreenshot {
135176
$directory = Split-Path -Path $Path -Parent
136177
New-Item -Path $directory -ItemType Directory -Force | Out-Null
137178

138-
$bounds = [System.Windows.Forms.SystemInformation]::VirtualScreen
179+
$bounds = Get-PhysicalDesktopBounds
139180
$bitmap = [System.Drawing.Bitmap]::new($bounds.Width, $bounds.Height)
140181
$graphics = [System.Drawing.Graphics]::FromImage($bitmap)
141182
try {
142-
try {
143-
$graphics.CopyFromScreen($bounds.Left, $bounds.Top, 0, 0, $bounds.Size)
183+
$sourceDc = [ReleaseSmokeNativeMethods]::GetDC([IntPtr]::Zero)
184+
if ($sourceDc -eq [IntPtr]::Zero) {
185+
throw 'GetDC failed while capturing the desktop.'
144186
}
145-
catch {
146-
$sourceDc = [ReleaseSmokeNativeMethods]::GetDC([IntPtr]::Zero)
147-
if ($sourceDc -eq [IntPtr]::Zero) {
148-
throw
149-
}
150187

151-
$targetDc = $graphics.GetHdc()
152-
try {
153-
$srccopy = 0x00CC0020
154-
if (-not [ReleaseSmokeNativeMethods]::BitBlt($targetDc, 0, 0, $bounds.Width, $bounds.Height, $sourceDc, $bounds.Left, $bounds.Top, $srccopy)) {
155-
throw "BitBlt failed while capturing the desktop."
156-
}
157-
}
158-
finally {
159-
$graphics.ReleaseHdc($targetDc)
160-
[ReleaseSmokeNativeMethods]::ReleaseDC([IntPtr]::Zero, $sourceDc) | Out-Null
188+
$targetDc = $graphics.GetHdc()
189+
try {
190+
$srccopy = 0x00CC0020
191+
if (-not [ReleaseSmokeNativeMethods]::BitBlt($targetDc, 0, 0, $bounds.Width, $bounds.Height, $sourceDc, $bounds.Left, $bounds.Top, $srccopy)) {
192+
throw "BitBlt failed while capturing the desktop."
161193
}
162194
}
195+
finally {
196+
$graphics.ReleaseHdc($targetDc)
197+
[ReleaseSmokeNativeMethods]::ReleaseDC([IntPtr]::Zero, $sourceDc) | Out-Null
198+
}
163199

164200
$bitmap.Save($Path, [System.Drawing.Imaging.ImageFormat]::Png)
165201
}

0 commit comments

Comments
 (0)