问题
I want to take a screenshot of a very particular part of the screen. As if I just want to take a screenshot of an image in the centre of the screen. How can I do that? How can I setthe alignment to only crop a particular area. I got the code from stackoverflow only to take a screenshot:
[Reflection.Assembly]::LoadWithPartialName("System.Drawing")
function screenshot([Drawing.Rectangle]$bounds, $path) {
$bmp = New-Object Drawing.Bitmap $bounds.width, $bounds.height
$graphics = [Drawing.Graphics]::FromImage($bmp)
$graphics.CopyFromScreen($bounds.Location, [Drawing.Point]::Empty, $bounds.size)
$bmp.Save($path)
$graphics.Dispose()
$bmp.Dispose()
}
$bounds = [Drawing.Rectangle]::FromLTRB(0, 0, 1000, 900)
screenshot $bounds "C:\screenshot.jpeg"
If we could edit this.
回答1:
For craps and laughter I made something here that will make a screenshot of a rectangle that is centered on the screen.
$captureSize = @{ Height = 300; Width = 200}
$screenDimensions = Get-WmiObject -Class Win32_DesktopMonitor | Select-Object -First 1 ScreenWidth,ScreenHeight
$screenCenterPoint = @{FromTop = ($screenDimensions.ScreenHeight) / 2; FromLeft = ($screenDimensions.ScreenWidth) /2 }
$box = @{}
$box.Left = $screenCenterPoint.FromLeft - $captureSize.Width / 2
$box.Top = $screenCenterPoint.FromTop - $captureSize.Height / 2
$box.Right = $box.Left + $captureSize.Width
$box.Bottom = $box.Top + $captureSize.Height
$bounds = [Drawing.Rectangle]::FromLTRB($box.Left,$box.Top,$box.Right,$box.Bottom)
Set $captureSize
to the values that you want. Using the SO question that was linked by Alpha M Cubed Get Screen resolution using WMI/powershell in Windows 7 we populate the "primary screen" dimensions into $screenDimensions
. Using some simple math with no error detection what so ever we figure the location of the box relative to the center of the screen. The function and function call stay the same.
回答2:
The code already includes the solution, it takes a rectangle, $bounds. Move that to your preferred position. If you'd like to find the size of the monitor you can refer to this question: Get Screen resolution using WMI/powershell in Windows 7 I will also mention that the current code (I think) saves in BMP format, while you specified a .jpeg extension.
来源:https://stackoverflow.com/questions/26550642/powershell-screenshot-of-a-particular-part