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 | |
} | |
} | |
} |
What kind of images/pictures can this handle? PNG, JPG, others? Other limitations?
@eabase supported formats are those you can see under Remarks in https://learn.microsoft.com/en-us/dotnet/api/system.drawing.image.fromfile?view=windowsdesktop-9.0
Ok, so what happens when you try to resize it?
Example-1
:
You have a 100x100 and want to resize to 50x100.
- Does it just cut off the image or does it squeeze the pixels?
Example-2
:
You have a 100x100 and want to resize to 200x100.
- Does it pad it with black pixels, or stretch it out? (Or something else?)
Ok, so what happens when you try to resize it?
Example-1
: You have a 100x100 and want to resize to 50x100.
- Does it just cut off the image or does it squeeze the pixels?
Example-2
: You have a 100x100 and want to resize to 200x100.
- Does it pad it with black pixels, or stretch it out? (Or something else?)
@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.
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
What kind of images/pictures can this handle?
PNG, JPG, others?
Other limitations?