Board logo

标题: [问题求助] [已解决]PowerShell如何获取图片像素大小? [打印本页]

作者: DAIC    时间: 2013-8-28 20:48     标题: [已解决]PowerShell如何获取图片像素大小?

本帖最后由 DAIC 于 2013-8-29 17:08 编辑

分别获取长和宽
我要根据像素大小做进一步处理
作者: PowerShell    时间: 2013-8-29 10:26

答:搜msdn

又答:试试 System.Drawing.Bitmap类.
http://msdn.microsoft.com/zh-cn/ ... drawing.bitmap.aspx
(当然很多其他类也行,只不过这个类,基于.net2.0很多低版本win,甚至低于winxp也能正常使用)
位图由图形图像及其特性的像素数据组成。 可使用许多标准格式将位图保存到文件中。 GDI+ 支持下列文件格式:BMP、GIF、EXIF、JPG、PNG 和 TIFF。 有关支持的格式的更多信息,请参见位图类型。

里面有

有 构造函数
Bitmap(Image) 从指定的现有图像初始化 Bitmap 类的新实例。

有 Height,Width属性


我不厌其烦地同bat脚本人讲:powershell依赖.net ,功能强大,语法简洁,系统管理,各种常用的需求,样样都能..人家微软早做好了,只恨我们学的不深,可我们很多类不知道,不会用.
作者: DAIC    时间: 2013-8-29 11:21

回复 2# PowerShell


    不懂C#,可否给个PowerShell代码学习一下?
作者: PowerShell    时间: 2013-8-29 14:29

  1. [system.reflection.assembly]::loadwithpartialname("System.Drawing") | Out-Null
  2. $叫我板牙哥 = New-Object System.Drawing.Bitmap("d:\8dce33dfb918889c558abb9ea8f171c17ff917f2128b4-OuLxbs_fw580.jpg")
  3. $叫我板牙哥.height
  4. $叫我板牙哥.width
复制代码

作者: broly    时间: 2013-8-29 15:14

System.Drawing.Image比System.Drawing.Bitmap功能更强些
作者: DAIC    时间: 2013-8-29 15:35

回复 4# PowerShell


这个报错如何解决?

d:\Test>type test.ps1
$objPic = New-Object System.Drawing.Bitmap("a.jpg")
$objPic.height
$objPic.width

d:\Test>powershell -f test.ps1
New-Object : Cannot find type [System.Drawing.Bitmap]: make sure the assembly containing this type is loaded.
At D:\Test\test.ps1:1 char:21
+ $objPic = New-Object <<<<  System.Drawing.Bitmap("a.jpg")
    + CategoryInfo          : InvalidType: (:) [New-Object], PSArgumentException
    + FullyQualifiedErrorId : TypeNotFound,Microsoft.PowerShell.Commands.NewObjectCommand
作者: PowerShell    时间: 2013-8-29 16:30

本帖最后由 PowerShell 于 2013-8-29 16:38 编辑

抱歉 已更正
作者: DAIC    时间: 2013-8-29 17:03

回复 7# PowerShell


    成功了。非常感谢!
作者: DAIC    时间: 2013-8-29 17:07

回复 5# broly


换成System.Drawing.Image之后报错

d:\Test>type test.ps1
[system.reflection.assembly]::loadwithpartialname("System.Drawing") | Out-Null
$objPic = New-Object System.Drawing.Image("a.jpg")
$objPic.width
$objPic.height

d:\Test>powershell -f test.ps1
New-Object : Constructor not found. Cannot find an appropriate constructor for type System.Drawing.Image.
At D:\Test\test.ps1:2 char:21
+ $objPic = New-Object <<<<  System.Drawing.Image("a.jpg")
    + CategoryInfo          : ObjectNotFound: (:) [New-Object], PSArgumentException
    + FullyQualifiedErrorId : CannotFindAppropriateCtor,Microsoft.PowerShell.Commands.NewObjectCommand
作者: broly    时间: 2013-8-29 19:50

  1. $objPic = New-Object System.Drawing.Image("a.jpg")
复制代码
改为
  1. $objPic = [System.Drawing.Image]::FromFile("a.jpg")
复制代码
System.Drawing.Image 支持位图和metafile文件
作者: DAIC    时间: 2013-8-30 13:28

回复 10# broly


成功了,多谢!
  1. [system.reflection.assembly]::loadwithpartialname("System.Drawing") | Out-Null
  2. $objPic = [System.Drawing.Image]::FromFile("a.bmp")
  3. $objPic.width
  4. $objPic.height
复制代码





欢迎光临 批处理之家 (http://bbs.bathome.net/) Powered by Discuz! 7.2