Created
December 12, 2022 15:53
-
-
Save santisq/3f918f707cb50560b7d7ab95a6db406e to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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
@eabase I have no idea 🤣 this was written 3 years ago, didn't put too much thought into it. You will have to test and see what happens.