Found on the Internet are mostly through string operations to find the IMG tag, this way to operate more cumbersome, and the code looks more tired. The method I use here is to load a page by WebBrowser, and then htmldocument the class to manipulate the steps that omit string manipulation, Call getElementsByTagName directly to return all the picture addresses to a HtmlElementCollection object.
The code is as follows:
Copy Code code as follows:
Using System;
Using System.Collections.Generic;
Using System.Linq;
Using System.Text;
Using System.Text.RegularExpressions;
Using System.Net;
Using System.IO;
Using System.Windows.Forms;
Namespace WindowsFormsApplication1
{
public class Gatherpic
{
private string Savepath;
private string GetUrl;
Private WebBrowser WB;
private int iimgcount;
Initialization parameters
Public Gatherpic (String sweburl, String ssavepath)
{
This.geturl = Sweburl;
This.savepath = Ssavepath;
}
Start collecting
public bool Start ()
{
if (Geturl.trim (). Equals (""))
{
MessageBox.Show ("Where the shrimp did not even lose the site!") ");
return false;
}
THIS.WB = new WebBrowser ();
This.wb.Navigate (GETURL);
Delegate Events
this.wb.DocumentCompleted + = new System.Windows.Forms.WebBrowserDocumentCompletedEventHandler (documentcompleted);
return true;
}
Webbrowser.documentcompleted Delegate Event
private void DocumentCompleted (object sender, WebBrowserDocumentCompletedEventArgs e)
{
In the page the frame iframe load completes does not drop uses the Searchimglist ()
if (E.url!= WB. Document.url) return;
Searchimglist ();
}
Check out all pictures and collect them locally
public void Searchimglist ()
{
String Simgurl;
Get all picture addresses
HtmlElementCollection Elemcoll = This.wb.Document.GetElementsByTagName ("img");
This.iimgcount = Elemcoll.count;
foreach (HtmlElement elem in Elemcoll)
{
Simgurl = Elem. GetAttribute ("src");
Call Save remote picture function
Saveimagefromweb (Simgurl, This.savepath);
}
}
Save remote picture function
public int Saveimagefromweb (string imgurl, String path)
{
String imgname = Imgurl.tostring (). Substring (Imgurl.tostring (). LastIndexOf ("/") + 1);
Path = path + "\ \" + imgname;
String defaulttype = ". jpg";
string[] Imgtypes = new string[] {". jpg", ". jpeg", ". png", ". gif", ". bmp"};
String imgtype = Imgurl.tostring (). Substring (Imgurl.tostring (). LastIndexOf ("."));
foreach (String it in Imgtypes)
{
if (Imgtype.tolower (). Equals (IT))
Break
if (IT). Equals (". bmp"))
Imgtype = DefaultType;
}
Try
{
HttpWebRequest request = (HttpWebRequest) webrequest.create (Imgurl);
Request. useragent = "mozilla/6.0" (MSIE 6.0; Windows NT 5.1; Natas.robot) ";
Request. Timeout = 10000;
WebResponse response = Request. GetResponse ();
Stream stream = Response. GetResponseStream ();
if (response. Contenttype.tolower (). StartsWith ("image/"))
{
byte[] Arraybyte = new byte[1024];
int imglong = (int) Response. ContentLength;
int l = 0;
CreateDirectory (path);
FileStream FSO = new FileStream (path, filemode.create);
while (L < Imglong)
{
int i = stream. Read (arraybyte, 0, 1024);
Fso. Write (arraybyte, 0, I);
L + = i;
}
Fso. Close ();
Stream. Close ();
Response. Close ();
return 1;
}
Else
{
return 0;
}
}
catch (WebException)
{
return 0;
}
catch (UriFormatException)
{
return 0;
}
}
}
}
-----------------Calling code--------------------
Gatherpic gatherpic = new Gatherpic ("http://www.baidu.com", "C:\test");
Please make sure the test path exists under C:\
Gatherpic.start ()