Sunday, October 27, 2024
Powershellf script to capture screnshot
# Load necessary assemblies
Add-Type -AssemblyName PresentationCore
Add-Type -AssemblyName System.Windows.Forms
Add-Type -AssemblyName System.Drawing
# Define the screenshots folder path and ensure it exists
$desktopPath = Join-Path -Path $env:USERPROFILE -ChildPath "Desktop\screenshots"
if (!(Test-Path -Path $desktopPath)) {
New-Item -ItemType Directory -Path $desktopPath | Out-Null
}
# Main loop to check for Shift key press
while ($true) {
# Check if either Shift key is pressed
if ([System.Windows.Input.Keyboard]::IsKeyDown([System.Windows.Input.Key]::LeftShift) -or
[System.Windows.Input.Keyboard]::IsKeyDown([System.Windows.Input.Key]::RightShift)) {
Write-Output "Shift key is pressed."
# Generate a unique file path with a timestamp
$timestamp = (Get-Date).ToString("yyyyMMdd_HHmmss")
$filePath = Join-Path -Path $desktopPath -ChildPath "screenshot_$timestamp.png"
Write-Output "Screenshot will be saved to: $filePath"
# Get primary monitor dimensions
$screenWidth = [System.Windows.Forms.SystemInformation]::PrimaryMonitorSize.Width
$screenHeight = [System.Windows.Forms.SystemInformation]::PrimaryMonitorSize.Height
# Create a new bitmap and graphics object for screen capture
$bitmap = New-Object System.Drawing.Bitmap $screenWidth, $screenHeight
$graphics = [System.Drawing.Graphics]::FromImage($bitmap)
$graphics.CopyFromScreen(0, 0, 0, 0, $bitmap.Size)
# Get mouse position
$mouseX = [System.Windows.Forms.Cursor]::Position.X
$mouseY = [System.Windows.Forms.Cursor]::Position.Y
$highlightRadius = 30 # Radius for the highlight circle
# Draw a circle around the mouse pointer
$highlightPen = New-Object System.Drawing.Pen([System.Drawing.Color]::Red, 3)
$graphics.DrawEllipse($highlightPen, $mouseX - $highlightRadius, $mouseY - $highlightRadius, $highlightRadius * 2, $highlightRadius * 2)
# Save the screenshot as a PNG file
$bitmap.Save($filePath, [System.Drawing.Imaging.ImageFormat]::Png)
# Release resources
$highlightPen.Dispose()
$graphics.Dispose()
$bitmap.Dispose()
Write-Output "Screenshot saved to $filePath"
# Delay to avoid multiple screenshots on a single key press
Start-Sleep -Milliseconds 500
} else {
# Optional: Remove this line if constant output isn't needed
Write-Output "Shift key is not pressed."
}
# Short delay before checking again
Start-Sleep -Milliseconds 5
}
Subscribe to:
Post Comments (Atom)
No comments:
Post a Comment