Esempio n. 1
0
        public static void CrawlDownload(Project project, List <string> urls, List <string> imgUrls)
        {
            //开始下载图片资源
            string imgUrl = project.URLData.ImgUrls.Dequeue();

            while (imgUrl != null)
            {
                ImgResource img = new ImgResource(DownloadService.DownloadImg(imgUrl), imgUrl);
                if (img != null)
                {
                    Stopwatch watch = new Stopwatch();
                    watch.Start();
                    project.ImgResourcesContainer.RowImages.Add(img);
                    watch.Stop();
                    img.DownloadTime = watch.ElapsedMilliseconds;
                    if (img.Img != null)
                    {
                        img.State = "Successful";
                    }
                    else
                    {
                        img.State = "Fail";
                    }
                    img.ResourceNumber = project.ImgResourcesContainer.RowImages.IndexOf(img) + 1;
                    string format;
                    ImageService.GetImageFormat(img.Img, out format);
                    img.PhotoFormat  = format;
                    img.ResourceSize = "待定,测试";
                }
                imgUrl = project.URLData.ImgUrls.Count > 0 ? project.URLData.ImgUrls.Dequeue() : null;
                //此处可添加事件,与前端互动
                CrawlerService.DownloadedImag(img.ResourceNumber, img.Url, img.PhotoFormat, img.ResourceSize, img.DownloadTime, img.State);
            }
        }
Esempio n. 2
0
        public static void ThreadCrawlDownload(Project project)
        {
            flag = false;
            List <Thread> Threads = new List <Thread>();
            int           count   = project.URLData.ImgUrls.Count();
            //开始下载图片资源
            int    finish = 0;
            string imgUrl = project.URLData.ImgUrls.Dequeue();

            while (finish < count)
            {
                if (flag)
                {
                    //foreach (Thread item in Threads)
                    //{
                    //    item.Abort();
                    //}
                    while (finish != Threads.Count)
                    {
                    }
                    break;
                }
                Thread thread = new Thread(() =>
                {
                    ImgResource img = new ImgResource(DownloadService.DownloadImg(imgUrl), imgUrl);
                    if (img != null)
                    {
                        Stopwatch watch = new Stopwatch();
                        watch.Start();
                        project.ImgResourcesContainer.RowImages.Add(img);
                        watch.Stop();
                        img.DownloadTime = watch.ElapsedMilliseconds;
                        if (img.Img != null)
                        {
                            img.State = "Successful";
                        }
                        else
                        {
                            img.State = "Fail";
                        }
                        img.ResourceNumber = project.ImgResourcesContainer.RowImages.IndexOf(img) + 1;
                        string format;
                        ImageService.GetImageFormat(img.Img, out format);
                        img.PhotoFormat = format;
                        if (img.Img != null)
                        {
                            img.ResourceSize = img.Img.Width + "*" + img.Img.Height;
                        }
                        else
                        {
                            img.ResourceSize = "无";
                        }
                        imgUrl = project.URLData.ImgUrls.Count > 0 ? project.URLData.ImgUrls.Dequeue() : null;
                        //此处可添加事件,与前端互动
                        CrawlerService.DownloadedImag(img.ResourceNumber, img.Url, img.PhotoFormat, img.ResourceSize, img.DownloadTime, img.State);
                        CrawlerService.ImgPreview();
                        finish++;
                    }
                });
                Threads.Add(thread);
                thread.Start();
                Thread.Sleep(200);
            }
        }