vb.netimageloadimagelist

VB.Net Imagelist Listview from URL


I have a Website displaying multiple jpg Images and I want to load them from URl and display them in my Imagelist.

I was able to do that with Images on my SSD Using the following code:

Dim count as integer = 0
Dim imgs as ImageList = New ImageList()
imgs.ImageSize = New Size(75, 75)

Dim files as String()
files = Directory.GetFiles("C:/image/folger")

ListView.SmallImageList = imgs  

For each x in files
   imgs.Images.Add(Image.FromFile(f))
   ListView.Items.Add("Image Number: " & count, count)
Next

Now I want to achieve the same thing but from an URL without downloading the files to my driver first. I got it 50% done using the following code:

    //Loading the Image from URL into Memory as Bitmap fuirst
    Dim tClient As WebClient = New WebClient
    Dim tImage As Bitmap = Bitmap.FromStream(New MemoryStream(tClient.DownloadData("https://url.to.image/1.jpg"))) 

    //Applying the Bitmap from Memory into the ImageList
    Dim imgs As ImageList = New ImageList()
    imgs.ImageSize = New Size(50, 50)
    imgs.Images.Add(tImage)

    ListView1.SmallImageList = imgs
    ListView1.Items.Add("Image " & vbCrLf & count.ToString, count)

This works great but as you can see the "Loop - For Each" is missing. I don't know how to request or collect each image on the website. As all images are "x.jpg" and x is always and only a number i thought about searching trough the websites sourcecode for each image until there is no image left. Would be great if someone could help me out with that.


Solution

  • You can download the photos until you get an http error 404 , meaning that the address is not existing .

    Dim pictIndx As Integer = 0
    Dim tClient As WebClient = New WebClient
    Dim imgs As ImageList = New ImageList()
    
    Try
       While True
           'Download until you get 404
           pictIndx += 1
           Dim tImage As Bitmap = Bitmap.FromStream(New MemoryStream(tClient.DownloadData("https://url.to.image/" & pictIndx & ".jpg"))) 
           'Download the image
           imgs.ImageSize = New Size(50, 50)
           imgs.Images.Add(tImage)
           'Add the image to the ListView
           ListView1.SmallImageList = imgs
           ListView1.Items.Add("Image " & vbCrLf & count.ToString, count)
    
       End While
    Catch ex as Exception
     'You downloaded all the photos
    End Try
    

    Let me know if this doesn't work