COMate: Getting images from a Webpage

Share your advanced PureBasic knowledge/code with the community.
User avatar
Kiffi
Addict
Addict
Posts: 1504
Joined: Tue Mar 02, 2004 1:20 pm
Location: Amphibios 9

COMate: Getting images from a Webpage

Post by Kiffi »

Hello,

this little code shows how to get images from a webpage:

Code: Select all

IncludePath #PB_Compiler_Home + "comate\"
XIncludeFile "comate.pbi"

EnableExplicit

Enumeration
  #frmMain
  #frmMain_WebGadget
  #frmMain_ListIconGadget
  #frmMain_SplitterGadget
EndEnumeration

Define WB.COMateObject
Define Image.COMateObject
Define Images.COMateEnumObject

Define ImageSource.s
Define ImageWidth.s 
Define ImageHeight.s

If OpenWindow(#frmMain, 0, 0, 600, 600, "Getting images from a Webpage", #PB_Window_SystemMenu | #PB_Window_ScreenCentered | #PB_Window_Invisible) 
  
  WebGadget(#frmMain_WebGadget, 0, 0, 0, 0, "http://www.purebasic.com/")
  
  ListIconGadget(#frmMain_ListIconGadget, 0, 0, 0, 0, "src", 400, #PB_ListIcon_GridLines | #PB_ListIcon_FullRowSelect)
  AddGadgetColumn(#frmMain_ListIconGadget, 1, "width", 50)
  AddGadgetColumn(#frmMain_ListIconGadget, 2, "height", 50)
  
  SplitterGadget(#frmMain_SplitterGadget, 0, 0, WindowWidth(#frmMain), WindowHeight(#frmMain), #frmMain_WebGadget, #frmMain_ListIconGadget)
  
  ; make sure that the page was completely loaded
  Repeat
    While WindowEvent() : Delay(1) : Wend
    If GetGadgetAttribute(#frmMain_WebGadget, #PB_Web_Busy) = 0 : Break : EndIf
  ForEver

  ; getting images
  WB = COMate_WrapCOMObject(GetWindowLong_(GadgetID(#frmMain_WebGadget), #GWL_USERDATA))
  If WB
    Images = WB\CreateEnumeration("document\images")
    If Images
      Image = Images\GetNextObject()
      While Image
        ImageSource =     Image\GetStringProperty("src")
        ImageWidth  = Str(Image\GetIntegerProperty("width"))
        ImageHeight = Str(Image\GetIntegerProperty("height"))
        If ImageSource
          AddGadgetItem(#frmMain_ListIconGadget, -1, ImageSource + #LF$ + ImageWidth + #LF$ + ImageHeight)
        EndIf
        Image\Release()
        Image = Images\GetNextObject()
      Wend
      Images\Release()
    EndIf
    WB\Release()
  EndIf
  
  HideWindow(#frmMain, #False)
  
  Repeat
  Until WaitWindowEvent() = #PB_Event_CloseWindow
  
EndIf
Greetings ... Kiffi
Hygge
srod
PureBasic Expert
PureBasic Expert
Posts: 10589
Joined: Wed Oct 29, 2003 4:35 pm
Location: Beyond the pale...

Post by srod »

Interesting, but it doesn't seem to get all images. Try http://www.purecoder.net/esgrid.htm
I may look like a mule, but I'm not a complete ass.
User avatar
Kiffi
Addict
Addict
Posts: 1504
Joined: Tue Mar 02, 2004 1:20 pm
Location: Amphibios 9

Post by Kiffi »

Hello srod,

ups, yes, you're right. I'm on it...

Greetings ... Kiffi
Hygge
User avatar
Kiffi
Addict
Addict
Posts: 1504
Joined: Tue Mar 02, 2004 1:20 pm
Location: Amphibios 9

Post by Kiffi »

strange...

this works (using a counter over images\length):

Code: Select all

CountImages = WB\GetIntegerProperty("document\images\length")
If CountImages
  For ImageCounter = 0 To CountImages - 1
    Image = WB\GetObjectProperty("document\images(" + Str(ImageCounter) + ")")
    If Image
      ImageSource =     Image\GetStringProperty("src")
      ImageWidth  = Str(Image\GetIntegerProperty("width"))
      ImageHeight = Str(Image\GetIntegerProperty("height"))
      If ImageSource
        AddGadgetItem(#frmMain_ListIconGadget, -1, ImageSource + #LF$ + ImageWidth + #LF$ + ImageHeight)
      EndIf
      Image\Release()
    EndIf
  Next
EndIf

this not (using CreateEnumeration):

Code: Select all

Images = WB\CreateEnumeration("document\images")
If Images
  Image = Images\GetNextObject()
  While Image
    ImageSource =     Image\GetStringProperty("src")
    ImageWidth  = Str(Image\GetIntegerProperty("width"))
    ImageHeight = Str(Image\GetIntegerProperty("height"))
    If ImageSource
      AddGadgetItem(#frmMain_ListIconGadget, -1, ImageSource + #LF$ + ImageWidth + #LF$ + ImageHeight)
    EndIf
    Image\Release()
    Image = Images\GetNextObject()
  Wend
  Images\Release()
EndIf
maybe you will see an error?

Greetings ... Kiffi
Hygge
srod
PureBasic Expert
PureBasic Expert
Posts: 10589
Joined: Wed Oct 29, 2003 4:35 pm
Location: Beyond the pale...

Post by srod »

Neither code works here with the EsGRID.htm page which should produce two images. Only one image is listed.
I may look like a mule, but I'm not a complete ass.
User avatar
pdwyer
Addict
Addict
Posts: 2813
Joined: Tue May 08, 2007 1:27 pm
Location: Chiba, Japan

Post by pdwyer »

Srod, the html source for your web page contains only 1 pic though
Paul Dwyer

“In nature, it’s not the strongest nor the most intelligent who survives. It’s the most adaptable to change” - Charles Darwin
“If you can't explain it to a six-year old you really don't understand it yourself.” - Albert Einstein
srod
PureBasic Expert
PureBasic Expert
Posts: 10589
Joined: Wed Oct 29, 2003 4:35 pm
Location: Beyond the pale...

Post by srod »

pdwyer wrote:Srod, the html source for your web page contains only 1 pic though
It does.... (It's been a while since I looked at it; but there are two images there!) Hang on...
I may look like a mule, but I'm not a complete ass.
srod
PureBasic Expert
PureBasic Expert
Posts: 10589
Joined: Wed Oct 29, 2003 4:35 pm
Location: Beyond the pale...

Post by srod »

Ah yes, the banner image is embedded within a css file - forgot about that!!! :)

Kiffi, is it possible to enumerate images residing within css files?
I may look like a mule, but I'm not a complete ass.
User avatar
Kiffi
Addict
Addict
Posts: 1504
Joined: Tue Mar 02, 2004 1:20 pm
Location: Amphibios 9

Post by Kiffi »

srod wrote:Kiffi, is it possible to enumerate images residing within css files?
yes, it is possible. But unfortunately i get no informations about the
imagesizes:

Code: Select all

IncludePath #PB_Compiler_Home + "comate"
XIncludeFile "comate.pbi"

EnableExplicit

Enumeration
  #frmMain
  #frmMain_WebGadget
  #frmMain_ListIconGadget
  #frmMain_SplitterGadget
EndEnumeration

Define WB.COMateObject
Define Image.COMateObject
Define Images.COMateEnumObject

Define ImageSource.s
Define ImageWidth.s 
Define ImageHeight.s

If OpenWindow(#frmMain, 0, 0, 600, 600, "Getting images from a Webpage", #PB_Window_SystemMenu | #PB_Window_ScreenCentered | #PB_Window_Invisible) 
  
  WebGadget(#frmMain_WebGadget, 0, 0, 0, 0, "http://www.purecoder.net/esgrid.htm")
  
  ListIconGadget(#frmMain_ListIconGadget, 0, 0, 0, 0, "src", 400, #PB_ListIcon_GridLines | #PB_ListIcon_FullRowSelect)
  AddGadgetColumn(#frmMain_ListIconGadget, 1, "width", 50)
  AddGadgetColumn(#frmMain_ListIconGadget, 2, "height", 50)
  
  SplitterGadget(#frmMain_SplitterGadget, 0, 0, WindowWidth(#frmMain), WindowHeight(#frmMain), #frmMain_WebGadget, #frmMain_ListIconGadget)
  
  ; make sure that the page was completely loaded
  Repeat
    While WindowEvent() : Delay(1) : Wend
    If GetGadgetAttribute(#frmMain_WebGadget, #PB_Web_Busy) = 0 : Break : EndIf
  ForEver
  
  ; detecting images
  WB = COMate_WrapCOMObject(GetWindowLong_(GadgetID(#frmMain_WebGadget), #GWL_USERDATA))
  If WB
    
    ; CreateEnumumeration failes??? 
    
    ; Images = WB\CreateEnumeration("document\images")
    ; If Images
    ; Image = Images\GetNextObject()
    ; While Image
    ; ImageSource =     Image\GetStringProperty("src")
    ; ImageWidth  = Str(Image\GetIntegerProperty("width"))
    ; ImageHeight = Str(Image\GetIntegerProperty("height"))
    ; If ImageSource
    ; AddGadgetItem(#frmMain_ListIconGadget, -1, ImageSource + #LF$ + ImageWidth + #LF$ + ImageHeight)
    ; EndIf
    ; Image\Release()
    ; Image = Images\GetNextObject()
    ; Wend
    ; Images\Release()
    ; EndIf
    
    ; --> images in document
    
    Define ImageCounter
    
    For ImageCounter = 0 To WB\GetIntegerProperty("document\images\length") - 1
      Image = WB\GetObjectProperty("document\images(" + Str(ImageCounter) + ")")
      If Image
        ImageSource =     Image\GetStringProperty("src")
        ImageWidth  = Str(Image\GetIntegerProperty("width"))
        ImageHeight = Str(Image\GetIntegerProperty("height"))
        If ImageSource
          AddGadgetItem(#frmMain_ListIconGadget, -1, ImageSource + #LF$ + ImageWidth + #LF$ + ImageHeight)
        EndIf
        Image\Release()
      EndIf
    Next
    
    
    ; --> images in CSS
    
    Define StyleCounter
    Define RuleCounter 
    Define RuleStyle.COMateObject
    
    For StyleCounter = 0 To WB\GetIntegerProperty("document\styleSheets\length") - 1
      For RuleCounter = 0 To WB\GetIntegerProperty("document\styleSheets(" + Str(StyleCounter) + ")\rules\length") - 1
        RuleStyle = WB\GetObjectProperty("document\styleSheets(" + Str(StyleCounter) + ")\rules(" + Str(RuleCounter) + ")\style")
        If RuleStyle
          If RuleStyle\GetStringProperty("backgroundImage") <> "" And RuleStyle\GetStringProperty("backgroundImage") <> "none" 
            AddGadgetItem(#frmMain_ListIconGadget, -1, RuleStyle\GetStringProperty("backgroundImage") + #LF$ + "N/A" + #LF$ + "N/A")
          EndIf
          RuleStyle\Release()
        EndIf
      Next
    Next
    
    WB\Release()
    
  EndIf
  
  HideWindow(#frmMain, #False)
  
  Repeat
  Until WaitWindowEvent() = #PB_Event_CloseWindow
  
EndIf
Greetings ... Kiffi
Hygge
srod
PureBasic Expert
PureBasic Expert
Posts: 10589
Joined: Wed Oct 29, 2003 4:35 pm
Location: Beyond the pale...

Post by srod »

Very nice Kiffi - very nice indeed! 8)
I may look like a mule, but I'm not a complete ass.
Post Reply