The source code is as follows:
Using System;
Using System. Collections. Generic;
Using System. ComponentModel;
Using System. Data;
Using System. Drawing;
Using System. Linq;
Using System. Text;
Using System. Windows. Forms;
Using System. Net;
Using System. Threading;
Using System. Text. RegularExpressions;
Using System. Diagnostics;
Using System. Collections;
Namespace WindowsFormsApplication14
{
Public partial class Form1: Form
{
Public struct param
{
Public int js;
Public string url;
}
Public delegate void listview1AddImg (String imgname, int js );
Public listview1AddImg myDelegate;
Public Form1 ()
{
InitializeComponent ();
This. imageList1.ImageSize = new Size (130,130 );
This. listView1.LargeImageList = this. imageList1;
MyDelegate = new listview1AddImg (listview1AddImgMethod );
}
P rivate void button#click (object sender, EventArgs e)
{
This. listView1.Items. Clear ();
String url = @ "http://image.baidu.com/I? Tn = baiduimage & ct = 201326592 & cl = 2 & lm =-1 & fr = & pv = & ic = 0 & z = 0 & word = % C3 % C0 % C5 % AE & s = 0 ";
Thread myThread1 = new Thread (downimg );
MyThread1.Start (object) url );
}
P rivate void downimg (object url)
{
String imgUrl = null;
Int js1 = 0;
Param param1 = new param ();
WebClient web1 = new WebClient ();
Byte [] data1 = web1.DownloadData (String) url );
String html = Encoding. Default. GetString (data1 );
// Analyze the image from the web page source code and input it to the thread pool
MatchCollection m1 = Regex. Matches (html, @ " [\ w:/\., & =] *) ", RegexOptions. IgnoreCase );
Foreach (Match m in m1)
{
Param1.js = js1;
Js1 ++;
ImgUrl = m. Groups ["smallpic"]. ToString ();
Param1.url = imgUrl;
ThreadPool. QueueUserWorkItem (new WaitCallback (dowork), (object) param1 );
}
}
P rivate void dowork (object list1)
{
// Download the image
String imgname = null;
Param param1 = new param ();
Param1 = (PARAM) list1;
WebClient web1 = new WebClient ();
// Bitmap BMP = new Bitmap (web1.openread (param1.url ));
Imgname = param1.js. tostring () + ". jpg ";
Web1.downloadfile (param1.url, application. startuppath + "\" + imgname );
This. listview1.invoke (this. mydelegate, new object [] {imgname, param1.js });
}
* ***** Void listview1AddImgMethod (String imgname, int js)
{
Try
{
This. imageList1.Images. Add (Image. FromFile (Application. StartupPath + "\" + imgname ));
This. listView1.Items. Add (imgname );
This. listView1.Items [js]. ImageIndex = js;
Debug. WriteLine (js );
Debug. writeline (imgname );
}
Catch (exception E)
{
Debug. writeline (E. Message );
}
}
}
}
Source code download