-
-
Save take4blue/fc70b17198d1707b33f36809ee29b809 to your computer and use it in GitHub Desktop.
PNGファイルを指定サイズのPNGファイルに変換する(アイコン作成用)
This file contains 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
# PNGファイルをアイコン用のビットマップサイズに変更する | |
# ファイルはDropして指定。アイコンサイズはテキストエリアの数値で指定。 | |
Add-Type -AssemblyName System.Drawing, System.Windows.Forms | |
function ResizeImage($InFileName, $WidthBits) | |
{ | |
# 画像ファイルの読みこみ | |
$fromBmp = [Drawing.Image]::FromFile($InFileName) | |
# 枠のけしこみ | |
$nowakuBmp = New-Object Drawing.Bitmap($fromBmp.Width, $fromBmp.Height) | |
$graphics = [Drawing.Graphics]::FromImage($nowakuBmp) | |
$rect = New-Object Drawing.Rectangle 1, 1, ($fromBmp.Width - 2), ($fromBmp.Height - 2) | |
$graphics.DrawImage($fromBmp, $rect, 1, 1, ($fromBmp.Width - 2), ($fromBmp.Height - 2), [Drawing.GraphicsUnit]::Pixel) | |
# リサイズ先のビットマップを作成 | |
# サイズは、$sizeで指定されている倍率にする | |
$toBmp = New-Object Drawing.Bitmap($WidthBits, $WidthBits) | |
# 属性の引継ぎ(Exifや回転情報などが引き継がれるはず) | |
foreach($prop in $fromBmp.PropertyItems) { | |
$toBmp.SetPropertyItem($prop); | |
} | |
# ソースイメージには、外枠が付与されているので、その部分を除去して指定サイズにリサイズする | |
# $rectがdestのどこに描画するかを示し、DrawImageの引数がsrcの何処を引用するかを示している | |
$graphics = [Drawing.Graphics]::FromImage($toBmp) | |
$rect = New-Object Drawing.Rectangle 0, 0, $WidthBits, $WidthBits | |
$graphics.DrawImage($nowakuBmp, $rect, 1, 1, ($fromBmp.Width - 2), ($fromBmp.Height - 2), [Drawing.GraphicsUnit]::Pixel) | |
# リサイズ後のビットマップを_sというファイル名を付けて保存する | |
$saveFile = [IO.Path]::Combine([IO.Path]::GetDirectoryName($InFileName), [IO.Path]::GetFileNameWithoutExtension($InFileName) + "_" + $WidthBits + [IO.Path]::GetExtension($InFileName)) | |
if (Test-Path $saveFile) { | |
Remove-Item $saveFile | |
} | |
$toBmp.Save($saveFile, [Drawing.Imaging.ImageFormat]::Png) # 明示的にPNGを指定しておく | |
} | |
# Drag & Dropでファイルを受け取る | |
function IsTargetFile ($filename) { | |
if ([IO.Path]::GetExtension($filename) -eq ".png") { | |
return $true | |
} | |
else { | |
return $false | |
} | |
} | |
$form = New-Object Windows.Forms.Form | |
$text = New-Object Windows.Forms.TextBox | |
$text.Multiline = $false | |
$text.Dock = [System.Windows.Forms.DockStyle]::Bottom | |
$text.Text = "64" | |
$text.Font = New-Object System.Drawing.Font("Lucida Console",24,[System.Drawing.FontStyle]::Regular) | |
$form.text = "Drag File" | |
$form.AllowDrop = $true | |
$form.Add_DragDrop({ | |
$width = [int]$text.Text | |
foreach ($filename in $_.Data.GetData([Windows.Forms.DataFormats]::FileDrop)) { | |
if (IsTargetFile($filename)) { | |
ResizeImage $filename $width | |
} | |
} | |
}) | |
$form.Add_DragOver({ | |
foreach ($filename in $_.Data.GetData([Windows.Forms.DataFormats]::FileDrop)) { | |
if (IsTargetFile($filename)) { | |
$_.Effect = [Windows.Forms.DragDropEffects]::All | |
} | |
} | |
}) | |
$form.Add_Shown({ | |
$form.Activate() | |
}) | |
$form.Controls.Add($text) | |
$form.ShowDialog() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment