I try to save clipboard data that contains the image, but I want to get more from history, so I need to know how to get the clipboard history.
function Save-ClipboardImages {
param (
[Parameter(Mandatory)]
[ValidateScript({
if(-Not ($_ | Test-Path) ){
throw "File or folder does not exist"
}
if($_ | Test-Path -PathType Leaf) {
throw "The Path argument must be a folder, not the file."
}
return $true
})][string]$outDir
)
Add-Type -AssemblyName System.Windows.Forms
$clipboard = [System.Windows.Forms.Clipboard]::GetDataObject()
if ($clipboard.ContainsImage()) {
$bitmap = [System.Drawing.Bitmap]$clipboard.getimage()
$memoryStream = New-Object System.IO.MemoryStream
$bitmap.Save($memoryStream, [System.Drawing.Imaging.ImageFormat]::Png)
$binaryData = $memoryStream.ToArray()
$md5 = [System.Security.Cryptography.MD5]::Create().ComputeHash($binaryData)
$md5HexString = [System.BitConverter]::ToString($md5) -replace "-", ""
$outPath = Join-Path $outDir "$md5HexString.png"
if (Test-Path $outPath) {
Write-Host "File already exits:" -NoNewLine
Write-Host $outPath -ForegroundColor Yellow
$memoryStream.Dispose()
$bitmap.Dispose()
return
}
$bitmap.Save($outPath, [System.Drawing.Imaging.ImageFormat]::Png)
Write-Output "clipboard content saved as $outPath"
$memoryStream.Dispose()
$bitmap.Dispose()
} else {
Write-Output "clipboard does not contains image data"
}
}