Site hosted by Angelfire.com: Build your free website today!

Download all images from a web page

Level: Intermediate

Download all images from a web page

This time we will save all images from a given web page.
For this project, we would use a TextBox, two Command buttons and a ListBox controls.
The form should look lithe this:



As always, we need to set reference to Microsoft HTML Object library and if you prefer not to use a WebBrowser control, a reference to Microsoft Internet Controls.
We will need a nice API function that we need to paste in General Declaratios section of our form (by the way, declare some variables that we will need later ;):
Option Explicit
Private Declare Function URLDownloadToFile Lib "urlmon" Alias "URLDownloadToFileA" (ByVal pCaller As Long, ByVal szURL As String, ByVal szFileName As String, ByVal dwReserved As Long, ByVal lpfnCB As Long) As Long

Dim WithEvents IE As InternetExplorer
Dim IEDoc As HTMLDocument
Dim bolDone As Boolean
After that, paste this code in Form_Load event, change the url contents of Text1 control to an url that you wish.
Private Sub Form_Load()
	Set IE = New InternetExplorer
	IE.navigate "about:blank"
End Sub
After our form is loaded, we have a hidden Internet Explorer running in the background. Command1 will do it navigate to the url that is showing in Text1. This is the code for Command1_Click event:
Private Sub Command1_Click()
bolDone = False
Command2.Enabled = False
With IE
   .navigate Text1.Text
   .Visible = True
End With
End Sub
After the page is fully downloaded, Command2 is enabled since we don't want to push the button before images are availables.
This is what happens at DocumentComplete event of our Internet Explorer object:
Private Sub IE_DocumentComplete(ByVal pDisp As Object, URL As Variant)
If (pDisp Is IE) Then
   Set IEDoc = IE.document
   Command2.Enabled = True
   bolDone = True
End If
End Sub
Document is complete so we could do our job right now pushing Command2 button:
Private Sub Command2_Click()
Dim img As IHTMLImgElement
Dim ImgName As String,sPath as string

sPath="c:\images\"
List1.Clear
if dir$("c:\images",vbDirectory) = "" then mkdir sPath
For Each img In IEDoc.images
   With img
      ' Extract the name of image from the url:
      ImgName = Mid$(.src, InStrRev(.src, "/", , vbTextCompare) + 1)
      List1.AddItem .src
      'example and function by Matthew Gates (Puff0rz@hotmail.com)
      Me.Caption ="downloading... " & .src
      DownloadFile .src, sPath & ImgName
	  Me.Caption = "Saved: " & .src
   End With
   Me.Caption = "Done."
Next
End Sub
DownloadFile is a nice function that i found at Allapi.net, here is the code for it:
Private Function DownloadFile(URL As String, LocalFilename As String) As Boolean
    Dim lngRetVal As Long
    lngRetVal = URLDownloadToFile(0, URL, LocalFilename, 0, 0)
    If lngRetVal = 0 Then DownloadFile = True
End Function
There is an enhancement for this function at Eduardo Morcillo's page. Try it!.
We have finished. Go to an Explorer window and look at "c:\images" folder.

Hope you have enjoyed that work as i did.
;)


©2003 - Richie Simonetti