Search code examples
c#web-servicesimagemp3

Retrieve album art/image from a web service


I have looked at the following question: Could someone provide a C# example using itemsearch from Amazon Web Services

It's out of date and not too useful.

What I want is to retrieve an image from a web service for the album art given artist, song title and/or album name information.

So far I'm digging through the following web service but I'm finding Amazon's offerings very convoluted for that I am looking to do: http://webservices.amazon.com/AWSECommerceService/AWSECommerceService.wsdl

Has anyone won this battle? Any simple ways of going about this?


Solution

  • I recently did this. I ended up using lastFM's API instead. Very simple to use.

    This is my full code, provided that you download their C# API and get an API account (which is free for non-commercial use, and instant). XXXXXX and YYYYYY is your lastFM login:

    public class LastFmAlbumArt
    {
        public static string AbsUrlOfArt(string album, string artist)
        {
            Lastfm.Services.Session session = new Lastfm.Services.Session("XXXXXX", "YYYYYY");
            Lastfm.Services.Artist lArtist = new Lastfm.Services.Artist(artist, session);
            Lastfm.Services.Album lAlbum = new Lastfm.Services.Album(lArtist, album, session);
    
            return lAlbum.GetImageURL();
        }
    
        public static Image AlbumArt(string album, string artist)
        {
            Stream stream = null;
            try
            {
                WebRequest req = WebRequest.Create(AbsUrlOfArt(album, artist));
                WebResponse response = req.GetResponse();
                stream = response.GetResponseStream();
                Image img = Image.FromStream(stream);
    
                return img;
            }
            catch (Exception e)
            {
                return null;
            }
            finally
            {
                if(stream != null)
                    stream.Dispose();
            }
        }
    }