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?

Dani AI

Generated

If you are using the WinForms WebBrowser control, the trick is to wait for the top-level page to finish, then crawl all frames (many CAPTCHAs are inside an iframe) and resolve each img src to an absolute URL. is right that parsing the DOM beats round-tripping with WebClient, and ’s approach can miss what is already in memory. Also note: DocumentCompleted fires once per frame; check the event URL so you only run after the main document completes. (learn.microsoft.com)

// wire this once (e.g., in Form_Load)
webBrowser1.DocumentCompleted += WebBrowser1_DocumentCompleted;

private void WebBrowser1_DocumentCompleted(object sender, WebBrowserDocumentCompletedEventArgs e)
{
    if (!e.Url.Equals(webBrowser1.Url)) return; // only when the top-level doc is ready

    var urls = GetAllImageUrls(webBrowser1.Document);
    // Optional: pick a likely captcha
    var captcha = urls.FirstOrDefault(u => u.IndexOf("captcha", StringComparison.OrdinalIgnoreCase) >= 0);
    // use urls/captcha as needed
}

private static List<string> GetAllImageUrls(HtmlDocument doc)
{
    var result = new List<string>();
    var seen = new HashSet<string>(StringComparer.OrdinalIgnoreCase);
    Collect(doc, seen, result);
    return result;
}

private static void Collect(HtmlDocument doc, HashSet<string> seen, List<string> result)
{
    if (doc == null) return;
    var baseUri = doc.Url;

    foreach (HtmlElement img in doc.GetElementsByTagName("img"))
    {
        var raw = img.GetAttribute("src");
        if (string.IsNullOrEmpty(raw)) continue;
        string abs; try { abs = new Uri(baseUri, raw).AbsoluteUri; } catch { abs = raw; }
        if (seen.Add(abs)) result.Add(abs);
    }

    foreach (HtmlWindow frame in doc.Window.Frames)
        try { Collect(frame.Document, seen, result); } catch { /* cross-domain frame */ }
}

Two gotchas: (1) if the captcha is served from a different domain, cross-frame scripting rules may block access to frame.Document, so the try/catch above is intentional. (2) Because the event fires per frame, the e.Url check prevents premature runs. (learn.microsoft.com)

Please respect the target site’s terms; scraping CAPTCHA content or automating around it can violate policies.

Recommended Answers

All 10 Replies

1. Highlight the image with your mouse.

2. Right Click

3.View Image info

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...

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);
}
else
{
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?

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) 
    imgTagsHtml.Add(img.OuterHtml);

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 , 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?

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.