PoshCode Logo PowerShell Code Repository

Resize-Image (modification of post by view diff)
embed code: <script type="text/javascript" src="http://PoshCode.org/embed/2000"></script>download | new post

A simple image resizer …

  1. ## NOTE: Destination must end in .bmp, .gif, .png, .wmp, .jpeg or .tiff
  2. param($source = "C:\Windows\Web\Wallpaper\Windows\img0.jpg", $destination = "$Home\Pictures\thumb0.png", $scale = 0.25)
  3. Add-Type -Assembly PresentationCore
  4.  
  5. ## Open and resize the image
  6. $image = New-Object System.Windows.Media.Imaging.TransformedBitmap (New-Object System.Windows.Media.Imaging.BitmapImage $source),
  7.                                                                    (New-Object System.Windows.Media.ScaleTransform $scale,$scale)
  8. ## Put it on the clipboard (just for fun)
  9. [System.Windows.Clipboard]::SetImage( $image )
  10.  
  11. ## Write out an image file:
  12. $stream = [System.IO.File]::Open($destination, "OpenOrCreate")
  13. $encoder = New-Object System.Windows.Media.Imaging.$([IO.Path]::GetExtension($destination).substring(1))BitmapEncoder
  14. $encoder.Frames.Add([System.Windows.Media.Imaging.BitmapFrame]::Create($image))
  15. $encoder.Save($stream)
  16. $stream.Dispose()

Submit a correction or amendment below (
click here to make a fresh posting)
After submitting an amendment, you'll be able to view the differences between the old and new posts easily.

Syntax highlighting:


Remember me