none
How to Programmatically Load Media Artwork for any Music Album, Video... Into a PictureBox and Get Correct Image Every Time from Google RRS feed

  • General discussion

  • Have you ever noticed that when the Windows Media Player loads the album artwork a large number of images were not found? If you are writing you own application and want to get the album art to display in a PictureBox well I wish you luck.

    Here is a very fast and extremely accurate method of grabbing the correct artwork and displaying in the PictureBox based on a Google Image search on the Title, Artist and Album information provided by WMPLib.

    I had to go Old School on this one after hours or trying to figure out a way without using any Active-X controls, just a WebClient and Strings.  I wanted the method to be completely thread safe as well without the need for delegates.  The demo application below allows you to type in any search string (i.e. Title, Artist and Album Strings all together) into the Search TextBox and click search.

    The image will be a picture of the Album, CD or DVD cover matching the media.

     You can easily concatenating this data by using WMPLib's 

    Player.currentMedia.getItemInfo("title") & " " &  UIForm.Player.currentMedia.getItemInfo("artist") & " " & UIForm.Player.currentMedia.getItemInfo("album")
    The resulting image will be displayed in the PictureBox.  (This is not a question it is only FYI)

    Imports System.Net
    
    Public Class Form1
    
        Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
            TextBoxSearch.Text = "put it on paper al green & ann nesby Body + Soul Duets Disc 1"
        End Sub
        Private Sub ButtonSearch_Click(sender As Object, e As EventArgs) Handles ButtonSearch.Click
    
            Dim client As New WebClient
            Dim photoLink As String = Nothing
            Dim strSeachableSongName As String = ""
            If TextBoxSearch.Text.Contains(" ") Then
                strSeachableSongName = EscapeURI("cd dvd " & TextBoxSearch.Text)
                strSeachableSongName += "&oq=" & strSeachableSongName
                strSeachableSongName = strSeachableSongName.Replace("%20", "+")
            End If
            Dim strURI As String = "https://www.google.com/search?biw=1920&bih=916&tbm=isch&sa=1&ei=rZkiXdmEBaWs_QbbvbKABA&q="
            strURI += strSeachableSongName
            strURI += "&gs_l=img.3...2705.2705..3141...0.0..0.58.58.1......0....1..gws-wiz-img.n7mM1Ax2zM8"
             Dim SourceCollection As String = client.DownloadString(New Uri(strURI))
            Dim intStartPosition As Integer = 0
            Dim intStartPositionSRC As Integer = 0
            Dim intEndPosition As Integer = 0
            Dim strHTML As String = Nothing
            Try
                intStartPosition = InStr(1, SourceCollection, "<img")
                strHTML = SourceCollection.Substring(intStartPosition - 1)
                intEndPosition = InStr(1, strHTML, "</img>")
                strHTML = strHTML.Substring(0, strHTML.Length - 6)
                intStartPosition = InStr(1, strHTML, "src=")
                strHTML = strHTML.Substring(intStartPosition - 1)
                intStartPosition = InStr(1, strHTML, "http")
                intEndPosition = InStr(intStartPosition, strHTML, """")
                Dim strImageLink As String = strHTML.Substring(intStartPosition - 1, intEndPosition - intStartPosition)
                PictureBoxCover.ImageLocation = strImageLink
            Catch ex As Exception
    
            End Try
    
        End Sub
    
        Private Function EscapeURI(pstrURI As String) As String
            EscapeURI = ""
            Dim strReturn As String = pstrURI
            Dim strReturn1 = strReturn.Replace("%", "%25")
            Dim strReturn2 = strReturn1.Replace("&", "%26")
            Dim strReturn3 = strReturn2.Replace("$", "%24")
            Dim strReturn4 = strReturn3.Replace("+", "%2b")
            Dim strReturn5 = strReturn4.Replace(" ", "%20")
            Dim strReturn6 = strReturn5.Replace("<", "%3c")
            Return strReturn6
        End Function
    
    End Class









    Monday, July 8, 2019 12:51 PM

All replies

  • Hi,

    see the following link:

    https://stackoverflow.com/questions/35103287/show-mp3-artwork-in-picturebox-c-sharp

    vb.net code:

    Private Sub coverButton_Click(ByVal sender As Object, ByVal e As EventArgs) Handles converButton.Click
        Dim dialogResult = openFileDialog.ShowDialog(Me)
    
        If dialogResult = DialogResult.OK Then
            Dim file As TagLib.File = TagLib.File.Create(openFileDialog.FileName)
            Dim mStream = New MemoryStream()
            Dim firstPicture = file.Tag.Pictures.FirstOrDefault()
    
            If firstPicture IsNot Nothing Then
                Dim pData As Byte() = firstPicture.Data.Data
                mStream.Write(pData, 0, Convert.ToInt32(pData.Length))
                Dim bm = New Bitmap(mStream, False)
                mStream.Dispose()
                coverPictureBox.Image = bm
            Else
            End If
        End If
    End Sub

    Best Regards,

    Alex


    MSDN Community Support Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.

    Tuesday, July 9, 2019 4:47 AM
    Moderator
  • Thank you Alex. That program requires you to open a local image file from your computer which means you had to search the web for the file, download it and then open the image in the application.

    My application does all of that programmatically based on a text string that you specify containing all of the words contained in the song or video Title, Artist and Album text.

    I switched this from a "Question" status to a "Discussion".  Sorry for any confusion.

    Wednesday, July 10, 2019 3:21 PM
  • Seems to work!

    Good job.

    I can think of lots of ways to go with it.

    PS Here is the thumbnail size image:

    Wednesday, July 10, 2019 5:02 PM
  • Thanks for posting the screenshot tommytwotrain. It is of course meant to appear as a smaller thumbnail.
    Wednesday, July 10, 2019 8:34 PM
  • Thanks for posting the screenshot tommytwotrain. It is of course meant to appear as a smaller thumbnail.

    Oh ok. I changed it above.

    :)

    Wednesday, July 10, 2019 11:40 PM