Add an Image from an URL in a ListView - Programmers Heaven

Howdy, Stranger!

It looks like you're new here. If you want to get involved, click one of these buttons!


Add an Image from an URL in a ListView

Nawiy0Nawiy0 France, LyonPosts: 1Member


I make a program to search images, I want to add an Image to an item from ListView.

How can I do?

I make an example:

Thank you, Nawiy0.

(Sorry, i'm French and my English is bad.)


  • IcepickleIcepickle GermanyPosts: 49Member

    Hi Nawiy0,

    here is a small implementation, it basically uses a custom service that services the results (SearchFlicker in this case) and a Downloading mechanisme that uses the HttpWebRequest / HttpWebResponse method (that would be the HttpDownload class)

    It then adds the images to an imagelist, which is attached to the ListView.

    Here is a small screenshot

    and the code of the mainform

    Imports System.Threading.Tasks
    Public Class frmSearchForm
        Protected searcher As New SearchFlicker()
        Public Delegate Sub SetSearchResultsHandler(imgList As ImageList, lvItems As ListViewItem())
        Public Delegate Sub AddListItemHandler(img As Image, lvItem As ListViewItem)
        Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
        End Sub
        Protected Sub SetSearchResults(imgList As ImageList, lvItems As ListViewItem())
            If lvSearchResults.InvokeRequired Then
                lvSearchResults.Invoke(New SetSearchResultsHandler(AddressOf SetSearchResults), imgList, lvItems)
            End If
            lvSearchResults.LargeImageList = imgList
            If lvItems IsNot Nothing Then
            End If
        End Sub
        Protected Sub AddListItem(img As Image, lvItem As ListViewItem)
            If lvSearchResults.InvokeRequired Then
                lvSearchResults.Invoke(New AddListItemHandler(AddressOf AddListItem), img, lvItem)
            End If
        End Sub
        Protected Async Function DoSearchAsync(term As String) As Task
            Dim imgList As New ImageList
            imgList.ImageSize = New Size(192, 128)
            imgList.ColorDepth = ColorDepth.Depth32Bit
            imgList.TransparentColor = Color.Transparent
            Dim imageIndex As Integer = 0
            SetSearchResults(imgList, Nothing)
            Dim results = Await searcher.SearchAsync(txtSearchTerm.Text)
            For Each item As FlickerResult In results
                Dim downloader As New HttpDownload()
                Dim result As HttpDownloadResult = Await downloader.DownloadFromAsync(item.ThumbImage).ConfigureAwait(False)
                If result.HasError Then
                    Continue For
                End If
                Dim lvi As New ListViewItem(item.Title, imageIndex)
                AddListItem(Image.FromFile(result.FileName), lvi)
                imageIndex = imageIndex + 1
        End Function
        Private Async Sub btnSearch_Click(sender As Object, e As EventArgs) Handles btnSearch.Click
            btnSearch.Enabled = False
            Await DoSearchAsync(txtSearchTerm.Text)
            btnSearch.Enabled = True
        End Sub
    End Class
Sign In or Register to comment.