使用c#从网页下载所有图像

时间:2014-12-04 08:35:16

标签: c# html

我正在尝试保存网页,以便我可以离线查看。我试图从网页上获取HTML,但我无法弄清楚如何获取所有图像。

这是我用来获取html的代码:

            HttpWebRequest request = (HttpWebRequest)WebRequest.Create(txtUrl.Text);
            HttpWebResponse responce = (HttpWebResponse)request.GetResponse();
            StreamReader sr = new StreamReader(responce.GetResponseStream());
            string responsehtml = sr.ReadToEnd();
            sr.Close();

            string path = @"C:\Users\Cargoguide\Pictures\test\test.htm";
            File.WriteAllText(path, responsehtml);
            Process.Start(path);

1 个答案:

答案 0 :(得分:2)

您可以让winform的WebBrowser控件完成这项工作。在解析网页后,您可以浏览图像元素以保存每个图像。

private void GetWebpage(string url)
{
    WebBrowser browser = new WebBrowser();
    browser.Navigate(url);
    browser.DocumentCompleted += new WebBrowserDocumentCompletedEventHandler(browser_DocumentCompleted);

}

void browser_DocumentCompleted(object sender, WebBrowserDocumentCompletedEventArgs e)
{
    var browser = (WebBrowser)sender;
    var client = new WebClient();
    foreach (var img in browser.Document.Images)
    {
        var image = img as HtmlElement;
        var src = image.GetAttribute("src").TrimEnd('/');
        if (!Uri.IsWellFormedUriString(src, UriKind.Absolute))
        {
            src = string.Concat(browser.Document.Url.AbsoluteUri, "/", src);
        }

        //Append any path to filename as needed
        var filename = new string(src.Skip(src.LastIndexOf('/')+1).ToArray());
        File.WriteAllBytes(filename, client.DownloadData(src));
    }
}