-
Notifications
You must be signed in to change notification settings - Fork 47
/
Copy pathGet-Screenshot.psm1
70 lines (60 loc) · 2.33 KB
/
Get-Screenshot.psm1
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
<#
Author: @strontic20
Website: strontic.com
Github: github.com/strontic/xcyclopedia
Adapted from:
https://rkeithhill.wordpress.com/2013/10/20/powershell-tidbit-capturing-a-screenshot-with-powershell/
https://foxdeploy.com/2017/09/09/use-powershell-to-take-automated-screencaps/
Synopsis: Take screenshot of specified process id.
License: MIT License; Copyright (c) 2020 strontic
#>
function Get-Screenshot($processid,$save_path) {
#param ([int]$procesid,[string]$save_path)
$src = @'
using System;
using System.Runtime.InteropServices;
namespace PInvoke
{
public static class NativeMethods
{
[DllImport("user32.dll")]
[return: MarshalAs(UnmanagedType.Bool)]
public static extern bool GetWindowRect(IntPtr hWnd, out RECT lpRect);
}
[StructLayout(LayoutKind.Sequential)]
public struct RECT
{
public int Left; // x position of upper-left corner
public int Top; // y position of upper-left corner
public int Right; // x position of lower-right corner
public int Bottom; // y position of lower-right corner
}
}
'@
Add-Type -TypeDefinition $src
Add-Type -AssemblyName System.Drawing
# Get a process object from which we will get the main window bounds
$iseProc = Get-Process -id $processid
$time
$bitmap = $g = $null
try {
$rect = new-object PInvoke.RECT
if ([PInvoke.NativeMethods]::GetWindowRect($iseProc.MainWindowHandle, [ref]$rect))
{
$width = $rect.Right - $rect.Left
$height = $rect.Bottom - $rect.Top
$bitmap = new-object System.Drawing.Bitmap $width,$height
$g = [System.Drawing.Graphics]::FromImage($bitmap)
$g."CopyFromScreen"($rect.Left, $rect.Top, 0, 0, $bitmap.Size,
[System.Drawing.CopyPixelOperation]::SourceCopy)
#$bitmap.Save("$save_path.bmp")
$bitmap.Save("$save_path.png",([system.drawing.imaging.imageformat]::Png));
#$bitmap.Save("$save_path.jpg",([system.drawing.imaging.imageformat]::Jpeg));
}
}
finally {
if ($bitmap) { $bitmap.Dispose() }
if ($g) { $g.Dispose() }
}
}
Export-ModuleMember -function Get-Screenshot