0

I've read some documentation and after some research im not sure if there is an easy way to do this.

I am using MusicBrainz to get an image of an artist. Which produces this URL

https://commons.wikimedia.org/wiki/File:Michael_Jackson_in_1988.jpg

I would like to download the image (which i have worked out) but i dont know how or what the correct procedure would be?

I have read a few links and all seem to be pretty dated including:

Download image from the site in .NET/C#

which doesnt answer my question as i already have a way of downloading the image. What im after in this case is a way of getting to the image URL using the above link which contains the download links.

I did read a link which is targeting an API but again that was dated and ready the help pages didnt mention this API (so it could have been third party).

I have everything to download the image, its just getting the correct URL which can be found on the link i posted above but i dont know how to extract the URL?

Community
  • 1
  • 1
Computer
  • 2,149
  • 7
  • 34
  • 71
  • possible duplicate of [Download image from the site in .NET/C#](http://stackoverflow.com/questions/3615800/download-image-from-the-site-in-net-c) – DLeh Apr 01 '15 at 19:42
  • Have you tried parsing the HTML to find the absolute source of the embedded image? – Vlad Apr 08 '15 at 17:34
  • Did a quick google but not entirely sure what steps i need to take? Could you possibly provide me a link to read up on? – Computer Apr 08 '15 at 17:43
  • 1
    @DLeh: have you read the question? – naveen Apr 08 '15 at 17:46

1 Answers1

0

You could use Html Agility Pack to get all the <a href elements in the html page and then look for the ones with an image type extension.

var document= new HtmlWeb().Load("https://commons.wikimedia.org/wiki/File:Michael_Jackson_in_1988.jpg");
var allhrefs = document.DocumentElement.SelectNodes("//a[@href]");

I'm sure they share a common structure, so I'll let you figure that out. At a quick glance it looks like the very first <a href points to the raw image.

var imagelink = allhrefs.FirstOrDefault();


EDIT: If you want to be more careful, you can filter then nodes by looking for the class='fullImageLink' div with id='file'. This seems to host the <a href to the original image.

Vlad
  • 1,889
  • 17
  • 33