淘小宝相册下载工具v2.0开发手记

图1:软件界面

软件用途:
这个小软件主要目的是为了方便批量下载淘小宝网络相册中的图片。
因为这个相册是收费的,空间有限,做服装等类型网店的朋友手中的产品图片往往过多,如果空间容量不够,解决办法当然是把过季的服装图片下载下来保存到硬盘上,再删除相册中的图片以节约空间。

目前淘宝店铺用得较广泛的支持外链的相册有:淘小宝,巴比豆,淘宝自己的相册空间,拍拍乐则已经倒闭了。

这个小程序很简单,原理无非是用正则式分析链接,然后按链接地址把图片下载下来。
以前开发的V1.0是易语言写的,有些用户报毒,我也很无耐,在易语言静态编译出来之前,这个是易语言的癌症。
所以现在V2.0换成了C#编写,彻底没有了这种烦恼。

代码如下,已经详细注释,不在浪费口条了:

namespace WindowsFormsApplication1
{
    public partial class Form1 : Form
    {
        //静态变量
        static String foldername = null; //保存用户输入的保存图片文件夹名称
        static int oversum = 0; //保存已经完成下载的计数
        static DateTime beginT; //下载开始的起始计时,用于完成下载统计总用时

        //用于更新标签文字的委托
        public delegate void delegateHandle(String msg1);
        public delegateHandle myDelegate;
        //用于更新进度条的委托
        public delegate void delegateHandle1(int value1);
        public delegateHandle1 myDelegate1;
       
        public Form1()
        {
            InitializeComponent();
            //绑定委托的成员函数
            myDelegate = new delegateHandle(refreshLable1);
            myDelegate1 = new delegateHandle1(refreshProgress1);
        }

        //委托成员函数refreshLable1
        p rivate void refreshLable1(String msg1)
        {
           this.label1.Text=msg1;
        }

        //委托成员函数refreshProgress1
        p rivate void refreshProgress1(int value1)
        {
            progressBar1.Value = value1;
            if (progressBar1.Maximum == oversum) //如果计数到达进度条最大值则转完成处理
            {
                //下载按钮恢复可用
                button1.Enabled = true;
                //静态变量清场
                foldername = null;
                oversum = 0;

                //更新lable1
                this.label1.Invoke(this.myDelegate, new object[] { "所有图片下载完成!耗时:"+DateTime.Now.Subtract(beginT)});

            }
        }
      
        p rivate void button2_Click(object sender, EventArgs e)
        {
            //用户点击帮助按钮
            Help.ShowHelp(this, "http://www.hackpig.cn/post/36.html");
        }

        p rivate void button1_Click(object sender, EventArgs e)
        {
            if (textBox1.Text.Length < 10)
            {
                MessageBox.Show("没有输入链接地址,或者输入非法!");
                return;
            }

           
            foldername = Interaction.InputBox("请输入保存图片文件夹名称", "提示", "temp", this.Top, this.Left);
            if (foldername == "")
            {
                return;
            }
            //Debug.WriteLine(foldername);
            //创建目录
            foldername = Application.StartupPath + "\\" + foldername;
            try
            {
                Directory.CreateDirectory(foldername);
            }
            catch (Exception m)
            {
                MessageBox.Show(m.Message);
                return;
            }

            //下载按钮变灰
            button1.Enabled = false;
            //记录下载开始时间
            beginT = DateTime.Now;


            //正则式分析链接
            String imgurl = null;
            int s1 = 0;
            int s2 = 0;

            MatchCollection m1 = Regex.Matches(textBox1.Text, @"<\w+\s+src=(?<url>[\w\.\d:/]*)>", RegexOptions.IgnoreCase);
            //设置进度条最大值
            this.progressBar1.Maximum = m1.Count;

            //控制线程数量
            if (checkBox1.Checked)
            {
                s1 = 20; s2 = 20;
            }
            else
            {
                s1 = 1; s2 = 1;
            }
            //置线程池最大线程数
            ThreadPool.SetMaxThreads(s1, s2);

            foreach (Match m in m1)
            {
                imgurl = m.Groups["url"].ToString();
                ThreadPool.QueueUserWorkItem(new WaitCallback(dowork), (object)imgurl);
            }
           
        }
      
        //线程池回调函数dowork
        p rivate void dowork(object url)
        {
            WebClient web1 = new WebClient();
            //从链接取图片链接名
            //http://www21.tx8.cn/photo/youran362/201012094446744.jpg
            String linkurl = (String)url;
            String imgname = null;

            try
            {
                int pi = linkurl.LastIndexOf("/", StringComparison.InvariantCultureIgnoreCase) + 1;
                imgname = foldername + "\\" + linkurl.Substring(pi, linkurl.Length - pi);
                //Debug.WriteLine(imgname);

                web1.DownloadFile(linkurl, imgname);
                Debug.WriteLine(linkurl);
                Debug.WriteLine(Thread.CurrentThread.ManagedThreadId);
                Debug.WriteLine(DateTime.Now.ToString("mm:ss"));

                //更新UI的Lable1标签文本
                String msg1 = linkurl.Substring(pi, linkurl.Length - pi) + "下载完成!";
                this.label1.Invoke(this.myDelegate, new object[] { msg1 });
                //更新UI进度条
                ++oversum;
                this.progressBar1.Invoke(this.myDelegate1, new object[] { oversum });
            }
            catch (Exception e2)
            {
                MessageBox.Show(e2.Message);
            }

        }

    }
}
 

下载淘小宝相册下载工具

posted @ 2010-02-15 12:39  猪悟能  阅读(347)  评论(0编辑  收藏  举报