• 设为首页
  • 点击收藏
  • 手机版
    手机扫一扫访问
    迪恩网络手机版
  • 关注官方公众号
    微信扫一扫关注
    公众号

C#多线程池演示例程--下载图片

原作者: [db:作者] 来自: [db:来源] 收藏 邀请

源代码如下:

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 button1_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);
            //从网页源码中分析出图片,并且投入线程池
            MatchCollection m1 = Regex.Matches(html, @"<img\s+name='pn\d+'\s+src=""(?<smallpic>[\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)
        {
            //下载图片
            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);
            }

        }
    }
}

源代码下载


鲜花

握手

雷人

路过

鸡蛋
该文章已有0人参与评论

请发表评论

全部评论

专题导读
上一篇:
我见过的最有艺术感的C代码发布时间:2022-07-14
下一篇:
object-c中NSString与扩展名操作发布时间:2022-07-14
热门推荐
阅读排行榜

扫描微信二维码

查看手机版网站

随时了解更新最新资讯

139-2527-9053

在线客服(服务时间 9:00~18:00)

在线QQ客服
地址:深圳市南山区西丽大学城创智工业园
电邮:jeky_zhao#qq.com
移动电话:139-2527-9053

Powered by 互联科技 X3.4© 2001-2213 极客世界.|Sitemap