Skip to content

Instantly share code, notes, and snippets.

@choutianxius
Created January 18, 2025 17:13
Show Gist options
  • Save choutianxius/e309f00cd12ed4547a14c83a0ba66983 to your computer and use it in GitHub Desktop.
Save choutianxius/e309f00cd12ed4547a14c83a0ba66983 to your computer and use it in GitHub Desktop.
Param(
[Parameter(Mandatory, Position = 0, HelpMessage = "Path to save PNG file")] [string] $SavePath
)
if (-not [System.IO.Directory]::Exists([System.IO.Path]::GetDirectoryName($SavePath))) {
Throw "Save directory doesn't exist."
}
Add-Type -AssemblyName System.Windows.Forms
if ([System.Windows.Forms.Clipboard]::ContainsData([System.Windows.Forms.DataFormats]::Bitmap)) {
if ([System.IO.File]::Exists($SavePath)) {
$Confirmation = Read-Host "The save path already exists. Are you sure that you want to override the existing file? [y/n]"
if (-not ($Confirmation.ToLower() -eq "y")) {
Write-Host "Abort"
Exit
}
}
try {
$image = [System.Windows.Forms.Clipboard]::GetData([System.Windows.Forms.DataFormats]::Bitmap)
$image.Save($SavePath, [System.Drawing.Imaging.ImageFormat]::Png)
[System.Windows.Forms.Clipboard]::Clear()
} catch {
Write-Error $_.Exception.Message
} finally {
$image.Dispose()
}
} else {
Write-Warning "No image found in the clipboard."
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment