用 PowerShell 把图片缩放为缩略图
仅用系统自带的 PowerShell 和 System.Drawing,一条命令把图片缩小为指定宽度的缩略图。
载入原图,按目标尺寸新建 Bitmap,用 DrawImage 以高质量插值绘制后保存位图即可。
01命令
命令
$ Add-Type -AssemblyName System.Drawing; $src=[System.Drawing.Image]::FromFile('C:\pics\photo.jpg'); $w=400; $h=[int]($src.Height*$w/$src.Width); $bmp=New-Object System.Drawing.Bitmap($w,$h); $g=[System.Drawing.Graphics]::FromImage($bmp); $g.InterpolationMode='HighQualityBicubic'; $g.DrawImage($src,0,0,$w,$h); $bmp.Save('C:\pics\photo-thumb.jpg'); $g.Dispose(); $bmp.Dispose(); $src.Dispose()02作用说明
通过 Graphics.DrawImage 把原图绘制到新 Bitmap 上,可以控制插值质量,比直接用带尺寸参数的 Bitmap 构造函数效果更好。高度按原始宽高比换算,图片不会被拉伸。把它放进 foreach 遍历 Get-ChildItem 就能批量处理整个文件夹,结束记得 Dispose 释放文件锁。
03风险
04撤销
✓无需撤销——该命令不会改变持久状态。
05来源
- Graphics.DrawImage Method (System.Drawing)依据: DrawImage is the documented way to render a source image at a new size onto a Bitmap, which is what makes this resize high quality.
- Image Class (System.Drawing)依据: Documents Image.FromFile for loading and the Width/Height properties used to compute the scaled aspect ratio.
最后验证: 2026-09-17 · 分类: 媒体