1 Sep 2011


This is an example of powershell script function that changes image format and size.
I've just updated the code from internet and using this on my powershell project.
$image is the file path of image file.


[System.Reflection.Assembly]::LoadWithPartialName("System.Drawing")
function Convert-Image{
    param($image,$toFormat,$saveTo,$smalldim,$bigdim)
 
    $baseName = [System.IO.Path]::GetFileNameWithoutExtension($image)
    $img = [System.Drawing.Image]::FromFile($image)
 
    "$image"
    if ($img.Width -gt $img.Height) {
        $width = $bigdim
        $height = $smalldim
    } else {
        $width = $smalldim
        $height = $bigdim
    }
 
    $ResizedImage = New-Object System.Drawing.Bitmap @($width, $height)
    $graphics = [System.Drawing.Graphics]::FromImage($ResizedImage)
    $graphics.DrawImage($img, 0, 0, $Width, $Height)
    $graphics.Dispose()
    $ResizedImage.save("$saveTo\$baseName.$toFormat",$toFormat)
}

2 comments:

Post a Comment