本帖最后由 CrLf 于 2014-9-12 16:10 编辑
这需要频繁调用外部命令,改用 vbs 处理可能会快些,提供一个函数示例:- Const testImage = "F:\test\测试\test.jpg"
-
- If cropImage(testImage,"png") Then
- MsgBox testImage & " 符合条件"
- Else
- MsgBox testImage & " 已被跳过"
- End If
-
- Function cropImage(imageFile,fileType)
- Dim Img,IP,fso
- Dim cropWidth,cropHeight,outFile,FormatID
-
- saveFile =imageFile
-
- Set Img = CreateObject("WIA.ImageFile")
- Set IP = CreateObject("WIA.ImageProcess")
- Set fso = WScript.CreateObject("Scripting.Filesystemobject")
-
- Img.LoadFile imageFile
-
- cropWidth = Img.Width And 1
- cropHeight = Img.Height And 1
-
- If cropWidth Or cropHeight Then
- cropImage = 1
-
- IP.Filters.Add IP.FilterInfos("Crop").FilterID
- IP.Filters(1).Properties("Right") = cropWidth
- IP.Filters(1).Properties("Bottom") = cropHeight
-
- Select Case LCase(fileType)
- Case "bmp": FormatID = "{B96B3CAB-0728-11D3-9D7B-0000F81EF32E}"
- Case "png": FormatID = "{B96B3CAF-0728-11D3-9D7B-0000F81EF32E}"
- Case "gif": FormatID = "{B96B3CB0-0728-11D3-9D7B-0000F81EF32E}"
- Case "jpeg","jpg": FormatID = "{B96B3CAE-0728-11D3-9D7B-0000F81EF32E}"
- Case "tiff","tif": FormatID = "{B96B3CB1-0728-11D3-9D7B-0000F81EF32E}"
- End Select
-
- If Len(FormatID)>0 Then
- IP.Filters.Add IP.FilterInfos("Convert").FilterID
- IP.Filters(2).Properties("FormatID").Value = FormatID
- outFile = Replace(imageFile&"*" , fso.GetExtensionName(imageFile)&"*" , fileType)
- End If
-
- Set Img = IP.Apply(Img)
- If imageFile = outFile Then fso.DeleteFile imageFile
- Img.SaveFile outFile
- End If
- End Function
复制代码
|