Skip to content

Instantly share code, notes, and snippets.

@santisq
Created December 12, 2022 15:53
Show Gist options
  • 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
}
}
}
@eabase
Copy link

eabase commented Feb 18, 2025

What kind of images/pictures can this handle?
PNG, JPG, others?
Other limitations?

@santisq
Copy link
Author

santisq commented Feb 18, 2025

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

@eabase
Copy link

eabase commented Feb 18, 2025

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?)

@santisq
Copy link
Author

santisq commented Feb 18, 2025

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