LoginSignup
12
8

More than 5 years have passed since last update.

PowerShellでクリップボードの画像をファイルに保存する

Last updated at Posted at 2016-12-17

デスクトップに "clip.png" として保存する例

Add-Type -AssemblyName System.Windows.Forms
$clipboardImage = [Windows.Forms.Clipboard]::GetImage()
if ($clipboardImage -ne $null)
{
  $outputFilePath = Join-Path (Join-Path $Env:UserProfile "Desktop") "clip.png"
  $clipboardImage.Save($outputFilePath)
}

解説

  1. まず、System.Windows.FormsAdd-Typeする。

    Add-Type -AssemblyName System.Windows.Forms
    
  2. 次に、クリップボードの画像を[Windows.Forms.Clipboard]::GetImage()で取得する(Bitmap型で返ってくる)。

    $clipboardImage = [Windows.Forms.Clipboard]::GetImage()
    
    • ここでの注意点:

      • クリップボードに画像が入っていないと、[Windows.Forms.Clipboard]::GetImage()$nullを返す。
        • このため、この例では$nullかどうかのチェックを入れている。
      • 追記: 画像の存在判定は[Windows.Forms.Clipboard]::ContainsImage()でも可能。

        if ([Windows.Forms.Clipboard]::ContainsImage())
        {
          $clipboardImage = [Windows.Forms.Clipboard]::GetImage()
          ...
        }
        
  3. 最後に、これをSave()する。

    $outputFilePath = Join-Path (Join-Path $Env:UserProfile "Desktop") "clip.png"
    $clipboardImage.Save($outputFilePath)
    
12
8
2

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
12
8