Howdy, Stranger!

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

Categories

Welcome to the new platform of Programmer's Heaven! We apologize for the inconvenience caused, if you visited us from a broken link of the previous version. The main reason to move to a new platform is to provide more effective and collaborative experience to you all. Please feel free to experience the new platform and use its exciting features. Contact us for any issue that you need to get clarified. We are more than happy to help you.

Add an Image from an URL in a ListView

Nawiy0Nawiy0 France, LyonPosts: 1Member

Hi,

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: http://imgur.com/PO5zwlZ

Thank you, Nawiy0.

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

Comments

  • 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 i.imgur.com/yUJWRDi.jpg

    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)
                Return
            End If
            lvSearchResults.Items.Clear()
            lvSearchResults.LargeImageList = imgList
            If lvItems IsNot Nothing Then
                lvSearchResults.Items.AddRange(lvItems)
            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)
                Return
            End If
            lvSearchResults.LargeImageList.Images.Add(img)
            lvSearchResults.Items.Add(lvItem)
        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)
            Threading.Thread.Sleep(250)
    
            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
            Next
        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.