如何仅使用 Powershell 将剪贴板中的图像发送到图床

如何仅使用 Powershell 将剪贴板中的图像发送到图床

我想使用电源外壳将剪贴板中的图像发送到图形床(例如http://stackoverflow.com/upload/image)。如果我在数学我可以从剪贴板中获取图像,然后使用此代码将图像发送到图片床

URLRead[HTTPRequest["http://stackoverflow.com/upload/image", <|
   "Body" -> {"image" -> imageInClipboard}|>], "Body"]

但这种方法效率很低。所以我希望通过电源外壳

$img=Get-Clipboard -Format image
Invoke-RestMethod -Uri http://stackoverflow.com/upload/image -ContentType "image/png" -Body $img

但它不起作用..并且降价结果如![](imagelink)预期的那样。有人可以帮我吗?

答案1

我为你写了这个脚本:

$img = Get-Clipboard -Format Image
If (-not $img) {Throw 'No image on the clipboard'}
$ms = New-Object System.IO.MemoryStream
$img.Save($ms, [System.Drawing.Imaging.ImageFormat]::Png)
$ms.Position = 0
Add-Type -AssemblyName System.Net.Http
$httpClientHandler = New-Object System.Net.Http.HttpClientHandler
$httpClient = New-Object System.Net.Http.HttpClient $httpClientHandler
$contentDispositionHeaderValue = New-Object System.Net.Http.Headers.ContentDispositionHeaderValue "form-data"
$contentDispositionHeaderValue.Name = "file"
$contentDispositionHeaderValue.FileName = 'image.png'
$streamContent = New-Object System.Net.Http.StreamContent $ms
$streamContent.Headers.ContentDisposition = $contentDispositionHeaderValue
$streamContent.Headers.ContentType = New-Object System.Net.Http.Headers.MediaTypeHeaderValue 'image/png'
$content = New-Object System.Net.Http.MultipartFormDataContent
$content.Add($streamContent)
$response = $httpClient.PostAsync('https://stackoverflow.com/upload/image?https=true', $content).Result
$response.Content.ReadAsStringAsync().Result -match '"(.*)"' | Out-Null
"![]($($matches[1]))"

首先,它将剪贴板上的图像转换为 PNG。然后利用一些 .NET Web 实用程序类来获取multipart/form-data结构设置恰到好处 - 本节改编自Mario Majčica 的博客文章。完成所有操作后,它会发出 POST 请求。最后,它使用正则表达式解析生成的小 HTML 文档以提取新上传图像的 URL,并将其打印到控制台。如果您想将 Markdown 片段直接复制到剪贴板,请粘贴| scb到最后一行。

要运行脚本,请将其保存为.ps1文件,例如seimage.ps1。如果您还没有这样做,请按照启用脚本部分中的说明进行操作PowerShell 标签 wiki。然后您可以在包含文件夹中打开 PowerShell 提示符并执行它:

.\seimage.ps1

答案2

我只是根据以下指导编写了代码以供阅读本·N

$img = Get-Clipboard -Format Image
If (-not $img) {
[void] [System.Reflection.Assembly]::LoadWithPartialName("System.Windows.Forms")
$objNotifyIcon = New-Object System.Windows.Forms.NotifyIcon 
$objNotifyIcon.Icon = [System.Drawing.SystemIcons]::Warning
$objNotifyIcon.BalloonTipIcon = "Error" 
$objNotifyIcon.BalloonTipText = 'No image on the clipboard' 
$objNotifyIcon.BalloonTipTitle = "File Not Found"
$objNotifyIcon.Visible = $True 
$objNotifyIcon.ShowBalloonTip(10000)
}
Else{
    $ms = New-Object System.IO.MemoryStream
    $img.Save($ms, [System.Drawing.Imaging.ImageFormat]::Png)
    $ms.Position = 0
    Add-Type -AssemblyName System.Net.Http
    $httpClientHandler = New-Object System.Net.Http.HttpClientHandler
    $httpClient = New-Object System.Net.Http.HttpClient $httpClientHandler
    $contentDispositionHeaderValue = New-Object System.Net.Http.Headers.ContentDispositionHeaderValue "form-data"
    $contentDispositionHeaderValue.Name = "file"
    $contentDispositionHeaderValue.FileName = 'image.png'
    $streamContent = New-Object System.Net.Http.StreamContent $ms
    $streamContent.Headers.ContentDisposition = $contentDispositionHeaderValue
    $streamContent.Headers.ContentType = New-Object System.Net.Http.Headers.MediaTypeHeaderValue 'image/png'
    $content = New-Object System.Net.Http.MultipartFormDataContent
    $content.Add($streamContent)
    $response = $httpClient.PostAsync('https://stackoverflow.com/upload/image?https=true', $content).Result
    $response.Content.ReadAsStringAsync().Result -match '"(.*)"' | Out-Null
    if ($args -eq "linkonly"){
        Set-Clipboard $matches[1]   
        }
    else{
        Set-Clipboard "![]($($matches[1]))"
    }
}

相关内容