ユーザ用ツール

サイト用ツール


powershell:form_resize_img

クリップボード画像リサイズツール

一つ上へ

Add-Type -AssemblyName System.Windows.Forms
Add-Type -AssemblyName System.Drawing
 
# フォーム作成
$form = New-Object System.Windows.Forms.Form
$form.Text = "Clipboard Image Viewer"
$form.Width = 800
$form.Height = 600
 
# PictureBox
$pictureBox = New-Object System.Windows.Forms.PictureBox
$pictureBox.Dock = "Fill"
$pictureBox.SizeMode = [System.Windows.Forms.PictureBoxSizeMode]::Zoom
$form.Controls.Add($pictureBox)
 
# ボタン(クリップボードから画像を貼り付け)
$buttonPaste = New-Object System.Windows.Forms.Button
$buttonPaste.Text = "Paste from Clipboard"
$buttonPaste.Dock = "Top"
$form.Controls.Add($buttonPaste)
 
# ボタン(アスペクト比を保ってリサイズ)
$buttonResize = New-Object System.Windows.Forms.Button
$buttonResize.Text = "Resize to fit 300x300"
$buttonResize.Dock = "Top"
$form.Controls.Add($buttonResize)
 
# クリップボードから貼り付け
$buttonPaste.Add_Click({
    if ([System.Windows.Forms.Clipboard]::ContainsImage()) {
        $image = [System.Windows.Forms.Clipboard]::GetImage()
        $pictureBox.Image = $image
    }
    else {
        [System.Windows.Forms.MessageBox]::Show("クリップボードに画像がありません。")
    }
})
 
# アスペクト比を維持してリサイズ
$buttonResize.Add_Click({
    if ($pictureBox.Image -ne $null) {
        $image = $pictureBox.Image
 
        # 元のサイズ
        $origWidth = $image.Width
        $origHeight = $image.Height
 
        # 最大サイズ
        $maxWidth = 300
        $maxHeight = 300
 
        # 縮尺を計算(小さい方に合わせる)
        $ratioX = $maxWidth / $origWidth
        $ratioY = $maxHeight / $origHeight
        $ratio = [Math]::Min($ratioX, $ratioY)
 
        $newWidth = [int]($origWidth * $ratio)
        $newHeight = [int]($origHeight * $ratio)
 
        # 新しいBitmapを作成して描画
        $resized = New-Object System.Drawing.Bitmap($newWidth, $newHeight)
        $g = [System.Drawing.Graphics]::FromImage($resized)
        $g.DrawImage($image, 0, 0, $newWidth, $newHeight)
        $g.Dispose()
 
        $pictureBox.Image = $resized
    }
    else {
        [System.Windows.Forms.MessageBox]::Show("画像がありません。")
    }
})
 
# フォームを表示
[void]$form.ShowDialog()
powershell/form_resize_img.txt · 最終更新: 2025/09/21 23:45 by mikoto