Resize-Image.ps1
[void][reflection.assembly]::LoadWithPartialName("System.Drawing")
[void][reflection.assembly]::LoadWithPartialName("System.Drawing.Imaging")
#===============================================================================
# Resize-Image: 画像ファイルをリサイズする(jpegのみ)
#
# パラメータ
# -srcFile : リサイズ元画像ファイルパス
# -dstFile : リサイズ後画像ファイルパス
# -width : リサイズ後の幅
# -height : リサイズ後の高さ
# -quality : 品質
# Default 既定の補間モード
# Low 低品質補間
# High 高品質補間
# NearestNeighbor 最近傍補間
# Bilinear 双一次補間
# Bicubic 双三次補間
# HighQualityBilinear 高品質双一次補間
# HighQualityBicubic 高品質双三次補間
# 使用例
# PS > Resize-Image "C:\Work\AAA.jpg" "C:\Work\BBB.jpg" 600 480 "HighQualityBicubic"
#
# copyright HIRO's.NET(http://hiros-dot.net/)
#===============================================================================
function global:Resize-Image
{
Param([string]$srcFile, [string]$dstFile, [int]$width=0, [int]$height=0, [string]$quality="Default")
#元画像ファイルの存在確認
if ( (Test-Path $srcFile) -eq $False )
{
Write-Host "リサイズ元画像が存在しません。パスを確認してください!!"
return
}
#元画像の読み込み
$srcImg = New-Object System.Drawing.Bitmap($srcFile)
$newWidth = 0
$newHeight = 0
#受け取った引数から、新しいサイズを決定する
if ( $width -gt 0 -and $height -eq 0 )
{
$newWidth = $width
$newHeight = $srcImg.Height / ($srcImg.Width / $newWidth)
}
elseif ( $height -gt 0 -and $width -eq 0 )
{
$newHeight = $height
$newWidth = $srcImg.Width / ($srcImg.Height / $newHeight)
}
elseif ( $width -gt 0 -and $height -gt 0 )
{
$newWidth = $width
$newHeight = $height
}
else
{
Write-Host "画像サイズを指定してください"
return
}
$dstImg = New-Object System.Drawing.Bitmap($srcImg, $newWidth, $newHeight)
$g = [System.Drawing.Graphics]::FromImage($dstImg)
#画像品質の設定
switch ($quality)
{
"Default" #既定の補間モード
{$g.InterpolationMode = [System.Drawing.Drawing2D.InterpolationMode]::Default}
"Low" #低品質補間
{$g.InterpolationMode = [System.Drawing.Drawing2D.InterpolationMode]::Low}
"High" #高品質補間
{$g.InterpolationMode = [System.Drawing.Drawing2D.InterpolationMode]::High}
"NearestNeighbor" #最近傍補間
{$g.InterpolationMode = [System.Drawing.Drawing2D.InterpolationMode]::NearestNeighbor}
"Bilinear" #双一次補間
{$g.InterpolationMode = [System.Drawing.Drawing2D.InterpolationMode]::Bilinear}
"Bicubic" #双三次補間
{$g.InterpolationMode = [System.Drawing.Drawing2D.InterpolationMode]::Bicubic}
"HighQualityBilinear" #高品質双一次補間
{$g.InterpolationMode = [System.Drawing.Drawing2D.InterpolationMode]::HighQualityBilinear}
"HighQualityBicubic" #高品質双三次補間
{$g.InterpolationMode = [System.Drawing.Drawing2D.InterpolationMode]::HighQualityBicubic}
default
{$g.InterpolationMode = [System.Drawing.Drawing2D.InterpolationMode]::Default}
}
#リサイズ画像を作成し保存する
$g.DrawImage($srcImg, 0, 0, $newWidth, $newHeight)
$dstImg.Save($dstFile, [System.Drawing.Imaging.ImageFormat]::Jpeg)
#オブジェクトの破棄
$srcImg.Dispose()
$dstImg.Dispose()
}