11
社区成员




作者:昨夜星辰
函数名:
SetImageSize 修改图片大小
参数定义:
Path 字符串型:图片文件的完整路径,比如"C:\test.bmp"
Width 整数型:图片宽
Height 整数型:图片高
IsPreserveAspectRatios 布尔型:是否保持图片纵横比,true为保持,false为不保持
Mode 整数型:影响参数Width和Height,0为像素模式,1为百分比模式
返回值:
无
源码:
VBSBegin
Function SetImageSize(Path, Width, Height, IsPreserveAspectRatios,Mode)
Dim fso, FileExists_ret
Dim Img,IP
Set fso = CreateObject("Scripting.FileSystemObject")
If (fso.FileExists(path))=true Then
FileExists_ret = 1
Else
FileExists_ret = 0
End If
If FileExists_ret = 1 Then
Set Img = CreateObject("WIA.ImageFile")
Set IP = CreateObject("WIA.ImageProcess")
Img.LoadFile Path
IP.Filters.Add IP.FilterInfos("Scale").FilterID
IP.Filters(1).Properties("PreserveAspectRatio") = IsPreserveAspectRatios
If Mode = 0 Then '像素
IP.Filters(1).Properties("MaximumWidth") = Width
IP.Filters(1).Properties("MaximumHeight") = Height
ElseIf Mode = 1 Then'百分比
IP.Filters(1).Properties("MaximumWidth") = CLng(Img.Width / 100 * Width)
IP.Filters(1).Properties("MaximumHeight") = CLng(Img.Height / 100 * Height)
End If
Set Img = IP.Apply(Img)
fso.DeleteFile Path
Img.SaveFile Path
Set IP = Nothing
Set Img = Nothing
End If
Set fso = Nothing
End Function
VBSEnd
Call SetImageSize("C:\test.bmp", 100, 100, false, 0)'修改图片为100X100,不保持纵横比