用 ASP 代码实现图片压缩的示例
|
admin
2025年1月1日 16:49
本文热度 137
|
以下是一个用 ASP(Active Server Pages)代码实现图片压缩的示例:
<%
Function CompressImage(SourceFile, TargetFile, MaxWidth, MaxHeight)
Set objImage = Server.CreateObject("WIA.ImageFile")
objImage.LoadFile(SourceFile)
Dim Width, Height
Width = objImage.Width
Height = objImage.Height
If Width > MaxWidth Or Height > MaxHeight Then
If Width > Height Then
Height = Height * MaxWidth / Width
Width = MaxWidth
Else
Width = Width * MaxHeight / Height
Height = MaxHeight
End If
End If
Set objEncoder = Server.CreateObject("WIA.ImageProcess")
objEncoder.Filters.Add objEncoder.FilterInfos("Convert").FilterID
objEncoder.Filters(1).Properties("FormatID").Value = "{B96B3CAE-0728-11D3-9D7B-0000F81EF32E}" 'JPEG format
objEncoder.Filters(1).Properties("Quality").Value = 75 'Quality level (adjust as needed)
Set objNewImage = objEncoder.Apply(objImage)
objNewImage.SaveFile TargetFile
Set objNewImage = Nothing
Set objEncoder = Nothing
Set objImage = Nothing
End Function
'Example usage
SourceFilePath = "C:\Path\To\Your\OriginalImage.jpg" '替换为原始图片路径
TargetFilePath = "C:\Path\To\Your\CompressedImage.jpg" '替换为压缩后图片保存路径
MaxWidth = 800
MaxHeight = 600
CompressImage SourceFilePath, TargetFilePath, MaxWidth, MaxHeight
%>
该文章在 2025/1/1 16:49:26 编辑过