Skip to content

Instantly share code, notes, and snippets.

@bu762
Created November 28, 2014 12:40
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save bu762/6e0f3668e59d4a932821 to your computer and use it in GitHub Desktop.
Save bu762/6e0f3668e59d4a932821 to your computer and use it in GitHub Desktop.
インターネット上の画像をクリップボードにコピーするコード(PowerShell版)
<#
インターネットの画像をクリップボードにペーストする
ついでに画像のURIもクリップボードにペーストする
#>
[VOID][System.Reflection.Assembly]::LoadWithPartialName('System.Net')
[VOID][System.Reflection.Assembly]::LoadWithPartialName('System.Drawing')
[VOID][System.Reflection.Assembly]::LoadWithPartialName('System.Windows.Forms')
if ($Args[0] -eq "") {
[System.Windows.Forms.MessageBox]::Show("対象ファイルのアドレス取得に失敗しました")
exit
}
$targetUrl = $Args[0]
<#
インターネットからファイルを読み取る
参考
http://www.atmarkit.co.jp/fdotnet/dotnettips/018loadbmp/loadbmp.html
#>
try {
$wcObj = New-Object System.Net.WebClient
$stream = $wcObj.OpenRead($targetUrl)
} catch [ArgumentNullException] {
[System.Windows.Forms.MessageBox]::Show("画像ファイルのアドレスが指定されていません")
$stream.Close()
exit
} catch [System.Net.WebException] {
[System.Windows.Forms.MessageBox]::Show($targetUrl + "からの読み取りに失敗しました")
$stream.Close()
exit
} catch [Exception] {
[System.Windows.Forms.MessageBox]::Show("想定外のエラーが発生しました")
$stream.Close()
exit
}
$bmpObj = New-Object System.Drawing.Bitmap($stream)
$stream.Close()
<#
データのクリップボードへのペースト
参考
http://mtgpowershell.blogspot.jp/2011/06/blog-post_29.html
#>
$dataObj = New-Object System.Windows.Forms.DataObject
$dataObj.SetText($targetUrl)
$dataObj.SetImage($bmpObj)
$cb = [Windows.Forms.Clipboard]
try {
$cb::SetDataObject($dataObj, $true)
} catch [ExternalException] {
[System.Windows.Forms.MessageBox]::Show("他のプロセスがクリップボードを使用しています")
} catch [ArgumentNullException] {
[System.Windows.Forms.MessageBox]::Show("ペーストすべきデータがありません")
} catch [ThreadStateException] {
[System.Windows.Forms.MessageBox]::Show("STAモードで実行してください")
} catch [Exception] {
[System.Windows.Forms.MessageBox]::Show("想定外のエラーが発生しました")
}
$wcObj = $null
$bmpObj = $null
$dataObj = $null
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment