Skip to content

Instantly share code, notes, and snippets.

@sheepla
Created January 10, 2021 12:57
Show Gist options
  • Save sheepla/33783a3ba52a1c1d269784d2593fa719 to your computer and use it in GitHub Desktop.
Save sheepla/33783a3ba52a1c1d269784d2593fa719 to your computer and use it in GitHub Desktop.
クリップボードに保存してある画像をファイルに出力する。
function Save-ImageFromClipboard
{
[CmdletBinding()]
param(
[Parameter(ValueFromPipelineByPropertyName=$false, ValueFromPipeline=$false, Position=0, Mandatory=$false, HelpMessage="Path of image file that you want to save")]
[String] $Path,
[Parameter(ValueFromPipelineByPropertyName=$false, ValueFromPipeline=$false, Mandatory=$false, HelpMessage="Open default image viewer")]
[Switch] $Invoke
)
# Generate path of image file
if ($null -eq $Path) {
$now = [DateTime]::Now.ToString("yyyyMMdd_HHMMss")
$saveFileName = "{0}.jpg" -f $now
$saveDir = Join-Path "${HOME}" "Pictures"
$savePath = Join-Path $saveDir $saveFileName
} else {
$savePath = $Path
}
# Get clipboard image object
Add-Type -AssemblyName System.Windows.Forms
$img = [System.Windows.Forms.Clipboard]::GetImage()
Write-Debug $img
if ($null -eq $img) {
Write-Error "Clipboard object is not image format or null. Please copy image and retry."
return
}
# Save image
try {
$img.Save("$savePath")
Write-Verbose "Saved image file: ${savePath}"
} catch { throw }
# If the switch is enabled, open the saved image in the default image viewer
if ($null -ne $Invoke) { Invoke-Item $savePath }
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment