copyright 1999 - 2014 by heinz prelle - hannover  - lenbachstraße 42 - www.visual-basic5.de | impressum
'Beispiel: VB .NET - Alle Bilder-Url's einer Webseite in einer ListBox anzeigen.
'
Option Explicit On
Option Strict On

Imports System.Net
Imports System.IO

Public Class Form1

    Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        With Me

            With Me.WebBrowser1
                .Navigate("http://images.google.de/images?hl=de&source=hp&q=Heinz+Prelle&gbv=2&aq=f&aqi=&aql=&oq=&gs_rfai=")
            End With
        End With
    End Sub

    Private Sub WebBrowser1_DocumentCompleted(ByVal sender As Object, ByVal e As System.Windows.Forms.WebBrowserDocumentCompletedEventArgs) _
            Handles WebBrowser1.DocumentCompleted
        Dim List As New List(Of String)(Me.ListImages("src", Me.WebBrowser1))
        If Not List Is Nothing Then
            Me.ListBox1.Items.AddRange(List.ToArray())
        End If
    End Sub

    Private Function ListImages(ByVal Tag As String, ByVal wb As WebBrowser) As List(Of String)
        Dim List As New List(Of String)
        Try
            Select Case Tag
                Case "src"
                    For Each ImageUrls As HtmlElement In wb.Document.Images
                        List.Add(ImageUrls.GetAttribute(Tag))
                    Next
                Case "href"
                    For Each ImageUrls As HtmlElement In wb.Document.Links
                        List.Add(ImageUrls.GetAttribute(Tag))
                    Next
            End Select
        Catch
            '
        End Try
        Return List
    End Function

    Private Function DownloadImageFromUrl(ByVal url As String) As Image
        Try
            Dim wc As WebClient = New WebClient()
            If Url <> "" Then
                If (Not wc Is Nothing) Then
                    Dim bt() As Byte = wc.DownloadData(Url)
                    Dim hStream As MemoryStream = New MemoryStream(bt)
                    If (Not hStream Is Nothing) Then
                        Dim img As Image = Image.FromStream(hStream)
                        If (Not img Is Nothing) Then
                            Return img
                        End If
                    End If
                End If
            End If
        Catch ex As WebException
            MessageBox.Show(ex.Message.ToString(), "WebException")
        Catch ex As Exception
            MessageBox.Show(ex.Message.ToString(), "Exception")
        End Try
        Return Nothing
    End Function

    Private Sub ListBox1_SelectedIndexChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) _
            Handles ListBox1.SelectedIndexChanged
        Dim Url As String = Me.ListBox1.SelectedItem.ToString()
        Dim img As Image = DownloadImageFromUrl(Url)
        If (Not img Is Nothing) Then
            Me.PictureBox1.Image = img
        End If
    End Sub

End Class
Sourcecode Visual Basic