Hey guys, i need to get the URL of an image on a webpage that im currently on in the webBrowser1 on my C# program. The image does not have an id and everytime i go to that page, the image is different, so i need to extract the URL of that image each time i go to that page. How would i do that?

Hey guys

and girls... ;-P

if you want to do that inside of your program the easiest way is probably to use DocumentText property... in that case you get the code of the current page (in string which is convenient in that case; to get html code use Document property) and you can parse it using regular expressions...

Edited 6 Years Ago by Szpilona: n/a

Use a WebClient
OpenRead the webpage
Look for the "<img" tag
Download whatever is there.
Hope for the best.

if he wants to find something on the page that is currently loaded in the WebBrowser control what to use WebClient for? it will unnecessary once again send HTML request to the server which can takes some time... so basically from my point of view it's waste of resources...

does this help? -

private string[] GetImageUrls()
if (webBrowser1.Document != null)
HtmlDocument doc = webBrowser1.Document;
string[] urls = (string[])Array.CreateInstance(Type.GetType("System.String"), doc.Images.Count);

foreach (HtmlElement imgElement in doc.Images)
urls[urls.Length] = imgElement.GetAttribute("src");
return (urls);
return (new string[0]);

taken from the msdn website [ http://msdn.microsoft.com/en-us/library/system.windows.forms.htmldocument.images.aspx ].

Can you post the html code for the "<img>" tag and the other tags around it?

Edited 6 Years Ago by farooqaaa: n/a

actually my cut-n-paste from msdn is absurdly overdone...

try this, maybe:

List<string> imgTagsHtml = new ArrayList<string>();
foreach (HtmlElement img in WebBrowser1.Document.Images) 

this should give you the complete html for every <img> tag in your document, going from the documentation.

also, as a side note, i'm pretty sure that this line:

string[] urls = (string[])Array.CreateInstance(Type.GetType("System.String"), doc.Images.Count);

can probably be safely replaced by

string[] urls = new string[doc.Images.Count];

something worth noting anyway.

actually my cut-n-paste from msdn is absurdly overdone...

eh... don't be so sure... as the code is from MSDN there can be some kind of performance differences... taking for example your little mistake (List<string> imgTagsHtml = new ArrayList<string>(); i guess typo) there is a big performance difference between List and ArrayList... according to microsoft and benchmarks List is much faster than ArrayList...
there can be such a difference also in that case...

hey guys...i have tried all of these and none of them seem to be working. I will tell you what specifically i am looking for. Go to Facebook.com, then fill in just the first page as if your making a new account. Then on the second page you will see a captcha. That is the image that I am trying to extract the 'src' of. Can anyone help me out with that?

This article has been dead for over six months. Start a new discussion instead.