Skip to content

Instantly share code, notes, and snippets.

@y-ack
Last active November 7, 2017 02:26
Show Gist options
  • Save y-ack/6e1844f70519609f19e1ea30c49d2ac4 to your computer and use it in GitHub Desktop.
Save y-ack/6e1844f70519609f19e1ea30c49d2ac4 to your computer and use it in GitHub Desktop.
upload screenshots with powershell
function Upload-ToKland {
param([String]$bucket = "",
[String]$Image,
[String]$Url = "https://kland.smilebasicsource.com/uploadimage")
$ImageName = [regex]::Match($Image,"[^\\/]+\.(png|PNG|gif|GIF|jpe?g|JPE?G)").Value
$Image = [System.Text.Encoding]::GetEncoding("iso-8859-1").GetString([IO.File]::ReadAllBytes($Image))
$ImageType = [System.Web.MimeMapping]::GetMimeMapping($Image)
$boundary = [guid]::NewGuid().ToString()
$PostBody = @"
--$boundary
Content-Disposition: form-data; name="image"; filename="$ImageName"
Content-Type: $ImageType
$Image
--$boundary
Content-Disposition: form-data; name="bucket"
$bucket
--$boundary--
"@
return Invoke-RestMethod -Method Post -Uri $Url -ContentType "multipart/form-data; boundary=$boundary" -Body $PostBody
}
function Take-Screenshot {
param([Drawing.Rectangle]$bounds = [System.Windows.Forms.Screen]::PrimaryScreen.Bounds,
[string]$path = "image.png"
)
$BMP = New-Object Drawing.Bitmap $bounds.width, $bounds.height
$Graphics = [Drawing.Graphics]::FromImage($BMP)
$Graphics.CopyFromScreen($bounds.Location, [Drawing.Point]::Empty, $bounds.size)
$BMP.Save($path, [System.Drawing.Imaging.ImageFormat]::Png)
$Graphics.Dispose()
$BMP.Dispose()
return $path
}
function KlandScreenshot([String]$Bucket) {
while ([System.Windows.Forms.UserControl]::MouseButtons -eq "None"){}
$pos1 = [System.Windows.Forms.Cursor]::Position
while ([System.Windows.Forms.UserControl]::MouseButtons -ne "None"){}
# wait for mouse release
while ([System.Windows.Forms.UserControl]::MouseButtons -eq "None"){}
$pos2 = [System.Windows.Forms.Cursor]::Position
$region = [Drawing.Rectangle]::new($pos1, $pos2 - $pos1)
$path = "$env:TEMP\klandimage.png"
$path = Take-Screenshot -bound $region -path $path
$file = Upload-ToKland -bucket $Bucket -image $path
Remove-Item $path
return $file
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment