Click here to Skip to main content
15,893,337 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hello everyone.

I've been working on a project that requires me to zero in on a specific image on a website and stick it into a WinForms PictureBox. I have installed the HtmlAgilityPack, and I have the XPath for the image I need. How can I use C# to pull this image from the web and put it in a PictureBox?

Thank you for your time.
Posted

1 solution

Hi, here is how you can do that:
C#
var htmlDocument = new HtmlWeb().Load("URL of website you are targeting...");
var imageNode = htmlDocument.DocumentNode.SelectSingleNode("XPath of image you are targeting...");
string imagePath = imageNode.Attributes["src"].Value;

var imageStream = HttpWebRequest.Create(imagePath).GetResponse().GetResponseStream();
this.pictureBox1.Image = Image.FromStream(imageStream);

Now I need to point out one thing regarding the imagePath, in the snippet above I just read the image's src attribute and create a web request for it, but in reality it is more likely that the image's src attribute will contain relative path so in that case you would need to make that imagePath into an absolute path because otherwise HttpWebRequest.Create(imagePath) will fail.
 
Share this answer
 
Comments
Member 11581301 11-Apr-15 11:46am    
I'm sorry, I'm a newbie to this. How do I make imagePath into an absolute path? Also, why is line 3 throwing an unhandled exception?
Mario Z 11-Apr-15 12:59pm    
First really don't be sorry, everyone starts at some point.
Now regarding the absolute path, let's say you are targeting some image on "http://www.somesite.com".
If the image uses an absolute path it would look something like this:

<img src="http://www.somesite.com/images/image.png" />

If it uses relative path it would look something like this:

<img src="/images/image.png" />

Do you see the difference?
In short a relative path is only a portion of the full path with which you are able to specify the location of some resource relative to another location (in this case relative to the targeted website).

Regarding the unhandled exception on line 3, the problem is probably with the imageNode, to be more specific I believe it is not an <img> tag and thus it does not have a "src" attribute.
Are you sure you're using a correct XPath to an image?
You should put a breakpoint and inspect the imageNode variable, is it an image HtmlNode?

Nevertheless if you still have difficulties can you post the URL of a website that you are targeting and specify which image you want to retrieve and which XPath you are using for it.
Member 11581301 11-Apr-15 13:33pm    
Thank you! Turns out my XPath was going to a div...

I managed to load the picture! Thank you for your help!
Johnny Ancich 28-Sep-22 9:37am    
Isn't the question more about getting the image if you do NOT know ANY path to the image?
Mario Z 29-Sep-22 3:09am    
First Johnny, please note that you usually won't get a reply when commenting on an answer or question from 7 years ago.
Nevertheless, the OP was looking for a "specific image" which means that he knows where the image element (the <img>) is located on that page.

If you don't know where the <img> element is, you can retrieve all the images with: htmlDocument.DocumentElement.SelectNodes("//img")

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900