Page 1 of 1

Read image length and Width from VB Script

Posted: Mon Jun 24, 2024 3:35 pm
by admin

Code: Select all

Option Explicit
dim objImage
set  objImage = new imageClass

objImage.filename = "Full_path_to_image_file"
wscript.echo objImage.filename
wscript.echo "Width: " & objImage.width
wscript.echo "Height: " & objImage.height


Class imageClass
  private initialized
  private imgWidth
  private imgHeight
  private imgFileName
  private hwArray(1)
  
  'Create a public method
  private Sub class_Initialize( )
   initialized = 0
   'wscript.echo "Class Initialized"
  end sub 

  public Property let filename(newFileName)
     if newFileName = "" then          
       Call Err.Raise(vbObjectError + 10, "Image Object", "Invalid Filename")
       wscript.quit
     end if

    setImageSize newFileName
    imgFilename = filename
    initialized=1
  end property 

  public Property get filename()
   if initialized = 0 then 
     filename = ""
   else 
    filename = imgFilename
   end if
  end property

 
     
  public Property get width()
   if initialized = 0 then 
     width = -1
   else 
    width = imgWidth
   end if
  end property
  
   public Property get height()
   if initialized = 0 then 
     height = -1
   else 
    height = imgHeight
   end if
  end property 
  
  Public Sub A_Public_Method(  )
     'Call the private method
      A_Private_Method
  End Sub

 
  
Private sub setImageSize(ImagePath)
Dim objImage   
 'wscript.echo "Setting W x H proprties"
 
 if FileExists(ImagePath) = False Then 
   wscript.echo "raising error Image Object", "File '" & ImagePath & "' Not Found"
  Call Err.Raise(vbObjectError + 10, "Image Object", "File '" & ImagePath & "' Not Found")
  wscript.quit
 end if 

 On Error Resume Next
  Set objImage = CreateObject("WIA.ImageFile")
  if objImage Is Nothing Then
    wscript.echo "Error Opening '" & ImagePath & "'"
    Call Err.Raise(vbObjectError + 10, "Image Object", "Error Opening '" & ImagePath & "'")
    wscript.quit
  end if
  
  On Error GoTo 0
  objImage.LoadFile ImagePath
  imgWidth = objImage.Width
  imgHeight= objImage.Height
    Set objImage = Nothing
'    GetImageSize = imgSize
End sub
 
'Function FileExists(FilePath As String) As Boolean
Private Function FileExists(FilePath)
 
    '--------------------------------------------------
    'Checks if a file exists (using the Dir function).
    '--------------------------------------------------
 
    On Error Resume Next
    if Len(FilePath) > 0 Then
        if Not Dir(FilePath, vbDirectory) = vbNullString Then FileExists = True
    End if
    On Error GoTo 0
 
End Function
  
End Class