@@ -77,6 +77,18 @@ public static class ReleaseSmokeNativeMethods
7777
7878 [DllImport("user32.dll")]
7979 public static extern bool ShowWindow(IntPtr hWnd, int nCmdShow);
80+
81+ [DllImport("user32.dll")]
82+ public static extern IntPtr GetDC(IntPtr hWnd);
83+
84+ [DllImport("user32.dll")]
85+ public static extern int ReleaseDC(IntPtr hWnd, IntPtr hDC);
86+
87+ [DllImport("gdi32.dll")]
88+ public static extern bool BitBlt(IntPtr hdcDest, int xDest, int yDest, int width, int height, IntPtr hdcSrc, int xSrc, int ySrc, int rasterOperation);
89+
90+ [DllImport("user32.dll")]
91+ public static extern bool PrintWindow(IntPtr hWnd, IntPtr hdcBlt, uint nFlags);
8092}
8193'@
8294 }
@@ -122,7 +134,28 @@ function Save-DesktopScreenshot {
122134 $bitmap = [System.Drawing.Bitmap ]::new($bounds.Width , $bounds.Height )
123135 $graphics = [System.Drawing.Graphics ]::FromImage($bitmap )
124136 try {
125- $graphics.CopyFromScreen ($bounds.Left , $bounds.Top , 0 , 0 , $bounds.Size )
137+ try {
138+ $graphics.CopyFromScreen ($bounds.Left , $bounds.Top , 0 , 0 , $bounds.Size )
139+ }
140+ catch {
141+ $sourceDc = [ReleaseSmokeNativeMethods ]::GetDC([IntPtr ]::Zero)
142+ if ($sourceDc -eq [IntPtr ]::Zero) {
143+ throw
144+ }
145+
146+ $targetDc = $graphics.GetHdc ()
147+ try {
148+ $srccopy = 0x00CC0020
149+ if (-not [ReleaseSmokeNativeMethods ]::BitBlt($targetDc , 0 , 0 , $bounds.Width , $bounds.Height , $sourceDc , $bounds.Left , $bounds.Top , $srccopy )) {
150+ throw " BitBlt failed while capturing the desktop."
151+ }
152+ }
153+ finally {
154+ $graphics.ReleaseHdc ($targetDc )
155+ [ReleaseSmokeNativeMethods ]::ReleaseDC([IntPtr ]::Zero, $sourceDc ) | Out-Null
156+ }
157+ }
158+
126159 $bitmap.Save ($Path , [System.Drawing.Imaging.ImageFormat ]::Png)
127160 }
128161 finally {
@@ -163,7 +196,29 @@ function Save-WindowScreenshot {
163196 $bitmap = [System.Drawing.Bitmap ]::new($width , $height )
164197 $graphics = [System.Drawing.Graphics ]::FromImage($bitmap )
165198 try {
166- $graphics.CopyFromScreen ($rect.Left , $rect.Top , 0 , 0 , [System.Drawing.Size ]::new($width , $height ))
199+ $targetDc = $graphics.GetHdc ()
200+ try {
201+ if (-not [ReleaseSmokeNativeMethods ]::PrintWindow($WindowHandle , $targetDc , 2 )) {
202+ $sourceDc = [ReleaseSmokeNativeMethods ]::GetDC([IntPtr ]::Zero)
203+ if ($sourceDc -eq [IntPtr ]::Zero) {
204+ throw " GetDC failed while capturing the UniGetUI window."
205+ }
206+
207+ try {
208+ $srccopy = 0x00CC0020
209+ if (-not [ReleaseSmokeNativeMethods ]::BitBlt($targetDc , 0 , 0 , $width , $height , $sourceDc , $rect.Left , $rect.Top , $srccopy )) {
210+ throw " BitBlt failed while capturing the UniGetUI window."
211+ }
212+ }
213+ finally {
214+ [ReleaseSmokeNativeMethods ]::ReleaseDC([IntPtr ]::Zero, $sourceDc ) | Out-Null
215+ }
216+ }
217+ }
218+ finally {
219+ $graphics.ReleaseHdc ($targetDc )
220+ }
221+
167222 $bitmap.Save ($Path , [System.Drawing.Imaging.ImageFormat ]::Png)
168223 }
169224 finally {
0 commit comments