C # multi-thread pool demo routine-download images

Source: Internet
Author: User

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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.