Skip to content

Instantly share code, notes, and snippets.

@santisq
Created December 12, 2022 15:53
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save santisq/3f918f707cb50560b7d7ab95a6db406e to your computer and use it in GitHub Desktop.
Save santisq/3f918f707cb50560b7d7ab95a6db406e to your computer and use it in GitHub Desktop.
using namespace System.Drawing
Add-Type -AssemblyName System.Drawing
function Resize-Picture {
[CmdletBinding()]
param(
[Parameter(Mandatory)]
[string] $SourcePath,
[Parameter(Mandatory)]
[int] $Width,
[Parameter(Mandatory)]
[int] $Height,
[Parameter(Mandatory)]
[string] $DestinationPath
)
end {
try {
$DestinationPath = $PSCmdlet.GetUnresolvedProviderPathFromPSPath($DestinationPath)
$source = Get-Item $SourcePath -ErrorAction Stop
$inStream = $source.OpenRead()
$sourceImage = [Image]::FromStream($inStream)
$newSize = [Size]::new($Width, $Height)
$destination = [Bitmap]::new($sourceImage, $newSize)
$destination.Save($DestinationPath)
}
catch {
$PSCmdlet.ThrowTerminatingError($_)
}
finally {
$sourceImage, $inStream, $destination | ForEach-Object Dispose
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment