Originally posted by zh159 at 2007-4-11 16:24:
May I ask, can baomaboy use VBS to obtain the image resolution and size (jpg, gif, bmp) (script)
Method 1, using the LoadPicture function:
Dim sFile,str
sFile = "a.bmp"
Set oPicture = LoadPicture(sFile)
str = "File:" & vbTab & sFile & vbCrLf
str = str & "Width:" & vbTab & Fix(oPicture.Width / 26.458) & vbCrLf
str = str & "Height:" & vbTab & Fix(oPicture.Height / 26.458)
Set oPicture = Nothing
WScript.Echo str
Method 2, using the powerful Shell.Application component:
Dim arrFile
Dim oFile,oDir,oShell
arrFile = MyGetFile()
Set oShell = CreateObject("Shell.Application")
Set oDir = oShell.NameSpace(arrFile(1) + "\")
Set oFile = oDir.ParseName(arrFile(0))
WScript.Echo oDir.GetDetailsOf(oFile,-1)
Set oFile = Nothing
Set oDir = Nothing
Set oShell = Nothing
'***********************************************************************************
'Get the file to operate, return an array containing the file name and path
'***********************************************************************************
Function MyGetFile()
On Error Resume Next
Dim strFile,objFso,objFile
If WScript.Arguments.Count < 1 Then
Set objDialog = CreateObject("UserAccounts.CommonDialog")
objDialog.Filter = "bmp file|*.bmp|jpg file|*.jpg|ico file|*.ico|all files|*.*"
objDialog.ShowOpen
strFile = objDialog.FileName
Set objDialog = Nothing
Else
strFile = WScript.Arguments(0)
end if
Set objFso = CreateObject("Scripting.FileSystemObject")
Set objFile = objFso.GetFile(strFile)
If Err Then
If Err.Number = 5 Then WScript.Quit
WScript.Echo Err.Description
Err.Clear
WScript.Quit
Else
MyGetFile = Array(objFile.Name,objFile.ParentFolder)
End If
Set objFile = Nothing
Set objFso = Nothing
End Function
Method 3, using Adodb.Stream to read the binary stream for analysis:
Dim sFile,str
Dim oStream
Dim bWidth,bHeight
sFile = "a.bmp"
Set oStream = CreateObject("Adodb.Stream")
With oStream
.Type = 1
.Open
.LoadFromFile sFile
End With
'If it is BMP format, then Position=18, read 4 bytes;
'If it is gif format, then it is Position=6, read 2 bytes;
'If it is png format, then Position=16, read 4 bytes
oStream.Position = 18
bWidth = oStream.Read(4)
bHeight = oStream.Read(4)
oStream.Close
str = "File:" & vbTab & sFile & vbCrLf
str = str & "Width:" & vbTab & Bin2Num(bWidth) & vbCrLf
str = str & "Height:" & vbTab & Bin2Num(bHeight)
Set oStream = Nothing
WScript.Echo str
'Convert binary stream to value
Private Function Bin2Num(binStr)
Dim i,numLen
numLen = Lenb(binStr)
For i = numLen To 1 Step -1
Bin2Num = Bin2Num * 256 + Ascb(Midb(binStr,i,1))
Next
End Function
Method one is undoubtedly the simplest, but the information that can be obtained is also the least.
Method two is relatively moderate. Compared with method one, more information can be obtained, and the interactivity is relatively stronger.
Method three should be the most flexible. The disadvantage is that various formats of images need to be processed separately, which requires more understanding of various formats.