I've got a script that works that runs on a scheduled task to snapshot the screen of a computer. Basically the computer runs a query at a regular interval that is pretty resource intensive on the server but has information that anyone might want to
see at a given time. So rather than having them generate the data on the fly, they get a html page that displays the screenshot of the last result set. The script is based on another one that I found online. Right now it saves in PNG format and
is about 1.8MB. I'm trying to get it to save in .JPG format so it is smaller.
This works:
$ScreenshotObject.Save($FilePath)
However this does not:
$ScreenshotObject.Save($JpgOut, Drawing.Imaging.ImageFormat.Jpeg))
I'm about 90% sure my problem lies in the Drawing.Imaging.ImageFormat.Jpeg part. I guess I'm not sure what the correct parameter is since there seem to be many things that can end in .ImageFormat.Jpeg. I've tried Drawing.Bitmap.ImageFormat.Jpeg.
I've tried with System at the beginning. I've also tried Drawing.Grapics.ImageFormat.Jpeg
I've also tried several variations on New-Object and various ways of trying to save it.
I guess I just can't figure out which is the right way.
The errors vary from Cannot find Type, or complaining that I passed a null value.
$ImageFormat = New-Object [Drawing.Imaging.ImageFormat]::Jpeg
$ImageFormat = New-Object [Drawing.Imaging]::ImageFormat.Jpeg
$ImageFormat = New-Object [Drawing.Imaging]::ImageFormat
$ScreenshotObject.Save("C:\Screenshot\Current.Jpg",$ImageFormat.Jpeg)
$ScreenshotObject.Save("C:\Screenshot\Current.Jpg",$ImageFormat)
And I've tried with and without:
Add-Type -Assembly System.Drawing.Imaging
Here's the full script. I've alternated between commenting out the first save and uncommenting the second.
Again, just saving it with a default path outputs it in PNG format and works. It's just 1.8MB is a lot to put in a web page the that keeps refreshing.
$Path = "c:\Screenshot"
$Current = "Current.Png"
$CurrentJpg = "Current.Jpg"
$JpgOut = (Join-Path $Path $CurrentJpg)
Function GenScreenshot
{
$ScreenBounds = [Windows.Forms.SystemInformation]::VirtualScreen
$ScreenshotObject = New-Object Drawing.Bitmap $ScreenBounds.Width, $ScreenBounds.Height
$DrawingGraphics = [Drawing.Graphics]::FromImage($ScreenshotObject)
$DrawingGraphics.CopyFromScreen( $ScreenBounds.Location, [Drawing.Point]::Empty, $ScreenBounds.Size)
$DrawingGraphics.Dispose()
$ScreenshotObject.Save($FilePath)
#$ScreenshotObject.Save("C:\Screenshot\Current.Jpg", Drawing.Imaging.ImageFormat.Jpeg))
$ScreenshotObject.Dispose()
}
Add-Type -Assembly System.Windows.Forms
$Time = (Get-Date)
[string] $FileName = "$($Env:ComputerName)"
$FileName += '-'
$FileName += "$($Time.Hour)"
$FileName += '-'
$FileName += "$($Time.Minute)"
$FileName += '.png'
[string] $FilePath = (Join-Path $Path $FileName)
{
Remove-Item $FilePath
}
GenScreenshot
Copy-Item $FilePath -destination (Join-Path $Path $Current)