PowerShell Image Resize Function
One for the toolbox. Some bits were done by someone else but I can’t remember who, I just parameterised the input, quality, size and output for easy use.
Read on…
1 2 3 4 5 |
$Date = Get-Date -Format "yyyy-MM-dd-HHmmss" $Screenshot = "myImage.jpg" $OutputFolder = "C:\ResizedScreenshot" ResizeImage $Screenshot 90 1280 ($OutputFolder+"\images\"+$Date+".jpg") |
Use as above. No commas between parameters!
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 |
Function ResizeImage() { param([String]$ImagePath, [Int]$Quality = 90, [Int]$targetSize, [String]$OutputLocation) Add-Type -AssemblyName "System.Drawing" $img = [System.Drawing.Image]::FromFile($ImagePath) $CanvasWidth = $targetSize $CanvasHeight = $targetSize #Encoder parameter for image quality $ImageEncoder = [System.Drawing.Imaging.Encoder]::Quality $encoderParams = New-Object System.Drawing.Imaging.EncoderParameters(1) $encoderParams.Param[0] = New-Object System.Drawing.Imaging.EncoderParameter($ImageEncoder, $Quality) # get codec $Codec = [System.Drawing.Imaging.ImageCodecInfo]::GetImageEncoders() | Where {$_.MimeType -eq 'image/jpeg'} #compute the final ratio to use $ratioX = $CanvasWidth / $img.Width; $ratioY = $CanvasHeight / $img.Height; $ratio = $ratioY if ($ratioX -le $ratioY) { $ratio = $ratioX } $newWidth = [int] ($img.Width * $ratio) $newHeight = [int] ($img.Height * $ratio) $bmpResized = New-Object System.Drawing.Bitmap($newWidth, $newHeight) $graph = [System.Drawing.Graphics]::FromImage($bmpResized) $graph.InterpolationMode = [System.Drawing.Drawing2D.InterpolationMode]::HighQualityBicubic $graph.Clear([System.Drawing.Color]::White) $graph.DrawImage($img, 0, 0, $newWidth, $newHeight) #save to file $bmpResized.Save($OutputLocation, $Codec, $($encoderParams)) $bmpResized.Dispose() $img.Dispose() } |
This is just what I am looking for but I’m not having any luck with it! I am getting “Exception calling “Save” with “3” argument(s): “A generic error occurred in GDI+.”
Any ideas? I am running Windows 7. Do I need to call/load any objects/cmdlets for this to run? Thanks!
Hi Mark, not that I’m aware of. The Add-Type call within the function handles the requirements. Perhaps with it being Windows 7 just check you have the latest PowerShell version and the latest .NET framework installed too. I probably writer this on Windows 8.1 so I assume that will have had .NET 4.5. Let me know if you’re still struggling after updating those and rebooting and I’ll spin up a Windows 7 machine to test.
Sorry Mark, I found this in Spam of all places – Akismet being a little over zealous!
Hi Mark, sorry for the late reply. I attempted to use the WordPress app on my phone but for some reason, the reply didn’t send – apologies! If you’re running on Windows 7, ensure you have the latest and greatest .NET 4.5.2 installed alongside the latest and greatest version of PowerShell. Version 4 should be OK as I’m pretty sure that’s what I developed it on. If you’re still struggling, let me know and I’ll see what I can do to help but please bear with me as I’m currently working internationally.