用 PowerShell 的 System.Drawing 读取图片尺寸
把任意 JPG/PNG/BMP 加载为 System.Drawing.Image 再读取宽高——脚本里快速核对图片尺寸的原生方法。
运行 `Add-Type -AssemblyName System.Drawing; $img=[System.Drawing.Image]::FromFile('<路径>'); $img.Width; $img.Height` 即可读取像素尺寸。
01命令
命令
$ Add-Type -AssemblyName System.Drawing; $img=[System.Drawing.Image]::FromFile('C:\Pictures\photo.jpg'); "$($img.Width) x $($img.Height)"; $img.Dispose()02作用说明
FromFile 只解码读取属性所需的部分,就能拿到 Width、Height、PixelFormat、文件大小等,比打开编辑器核对快得多。务必调用 Dispose()(或用 try/finally 包住),否则文件会被一直锁定,后续重命名、移动都会失败。用 Get-ChildItem *.jpg 循环可以批量生成尺寸报表;注意此方法需要 Windows PowerShell 5.1,或 PowerShell 7 下安装 Microsoft.Windows.Compatibility 包。
03风险
04撤销
✓无需撤销——该命令不会改变持久状态。
05来源
- Image Class (System.Drawing) | Microsoft Learn依据: Microsoft documents System.Drawing.Image with FromFile and Width/Height properties for reading image dimensions.
最后验证: 2026-09-17 · 分类: 媒体