Created
January 10, 2021 12:57
-
-
Save sheepla/33783a3ba52a1c1d269784d2593fa719 to your computer and use it in GitHub Desktop.
クリップボードに保存してある画像をファイルに出力する。
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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