Created
May 9, 2024 02:50
-
-
Save santisq/6c0fdbd6e2aec226be7caed0de100703 to your computer and use it in GitHub Desktop.
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
$PrintWindowCode = @' | |
using System; | |
using System.Runtime.InteropServices; | |
using System.Drawing; | |
using System.Drawing.Imaging; | |
public class ScreenCapture | |
{ | |
[DllImport("user32.dll")] | |
public static extern bool GetWindowRect(IntPtr hWnd, out Rect lpRect); | |
[DllImport("user32.dll")] | |
public static extern bool PrintWindow(IntPtr hWnd, IntPtr hdcBlt, int nFlags); | |
public Bitmap PrintWindow(IntPtr hwnd, string path, ImageFormat format) | |
{ | |
int PW_CLIENTONLY = 0x1; | |
int PW_RENDERFULLCONTENT = 0x2; | |
Rect rc; | |
Bitmap bmp; | |
GetWindowRect(hwnd, out rc); | |
using (bmp = new Bitmap(rc.Width, rc.Height, PixelFormat.Format32bppArgb)) | |
{ | |
using (Graphics gfxBmp = Graphics.FromImage(bmp)) | |
{ | |
IntPtr hdcBitmap = gfxBmp.GetHdc(); | |
try | |
{ | |
PrintWindow(hwnd, hdcBitmap, PW_CLIENTONLY | PW_RENDERFULLCONTENT); | |
} | |
finally | |
{ | |
gfxBmp.ReleaseHdc(hdcBitmap); | |
} | |
} | |
bmp.Save(path, format); | |
} | |
return bmp; | |
} | |
[StructLayout(LayoutKind.Sequential)] | |
public struct Rect | |
{ | |
private int _left; | |
private int _top; | |
private int _right; | |
private int _bottom; | |
public Rect(Rect Rectangle) | |
: this(Rectangle.Left, Rectangle.Top, Rectangle.Right, Rectangle.Bottom) | |
{ } | |
public Rect(int Left, int Top, int Right, int Bottom) | |
{ | |
_left = Left; | |
_top = Top; | |
_right = Right; | |
_bottom = Bottom; | |
} | |
public int X | |
{ | |
get { return _left; } | |
set { _left = value; } | |
} | |
public int Y | |
{ | |
get { return _top; } | |
set { _top = value; } | |
} | |
public int Left | |
{ | |
get { return _left; } | |
set { _left = value; } | |
} | |
public int Top | |
{ | |
get { return _top; } | |
set { _top = value; } | |
} | |
public int Right | |
{ | |
get { return _right; } | |
set { _right = value; } | |
} | |
public int Bottom | |
{ | |
get { return _bottom; } | |
set { _bottom = value; } | |
} | |
public int Height | |
{ | |
get { return _bottom - _top; } | |
set { _bottom = value + _top; } | |
} | |
public int Width | |
{ | |
get { return _right - _left; } | |
set { _right = value + _left; } | |
} | |
public Point Location | |
{ | |
get { return new Point(Left, Top); } | |
set | |
{ | |
_left = value.X; | |
_top = value.Y; | |
} | |
} | |
public Size Size | |
{ | |
get { return new Size(Width, Height); } | |
set | |
{ | |
_right = value.Width + _left; | |
_bottom = value.Height + _top; | |
} | |
} | |
public static implicit operator Rectangle(Rect Rectangle) | |
{ | |
return new Rectangle(Rectangle.Left, Rectangle.Top, Rectangle.Width, Rectangle.Height); | |
} | |
public static implicit operator Rect(Rectangle Rectangle) | |
{ | |
return new Rect(Rectangle.Left, Rectangle.Top, Rectangle.Right, Rectangle.Bottom); | |
} | |
public static bool operator ==(Rect Rectangle1, Rect Rectangle2) | |
{ | |
return Rectangle1.Equals(Rectangle2); | |
} | |
public static bool operator !=(Rect Rectangle1, Rect Rectangle2) | |
{ | |
return !Rectangle1.Equals(Rectangle2); | |
} | |
public override string ToString() | |
{ | |
return string.Format( | |
"{{ Left: {0}; Top: {1}; Right: {2}; Bottom: {3} }}", | |
_left, _top, _right, _bottom); | |
} | |
public override int GetHashCode() | |
{ | |
return ToString().GetHashCode(); | |
} | |
public bool Equals(Rect Rectangle) | |
{ | |
return Rectangle.Left == _left | |
&& Rectangle.Top == _top | |
&& Rectangle.Right == _right | |
&& Rectangle.Bottom == _bottom; | |
} | |
public override bool Equals(object Object) | |
{ | |
if (Object is Rect) | |
{ | |
return Equals((Rect)Object); | |
} | |
else if (Object is Rectangle) | |
{ | |
return Equals(new Rect((Rectangle)Object)); | |
} | |
return false; | |
} | |
} | |
} | |
'@ | |
Add-Type -AssemblyName System.Windows.Forms, System.Drawing | |
Add-Type $PrintWindowCode -ReferencedAssemblies @( | |
[System.Windows.Forms.Form].Assembly.Location | |
[System.Drawing.Graphics].Assembly.Location | |
if ($IsCoreCLR) { | |
$pwshLocation = Split-Path -Path ([psobject].Assembly.Location) -Parent | |
$pwshRefAssemblyPattern = [IO.Path]::Combine($pwshLocation, 'ref', '*.dll') | |
(Get-Item -Path $pwshRefAssemblyPattern).FullName | |
} | |
) | |
taskmgr | |
do { | |
$proc = Get-Process taskmgr | |
Start-Sleep 1 | |
} | |
while ($proc.MainWindowHandle -eq [System.IntPtr]::Zero) | |
$ScreenCapture = [ScreenCapture]::new() | |
$ScreenCapture.PrintWindow( | |
$proc.MainWindowHandle, | |
(Join-Path $pwd "taskmgr_screenshot_$([guid]::NewGuid()).png"), | |
[System.Drawing.Imaging.ImageFormat]::Png) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment