程序師世界是廣大編程愛好者互助、分享、學習的平台,程序師世界有你更精彩!
首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫知識
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
 程式師世界 >> 編程語言 >> .NET網頁編程 >> C# >> C#入門知識 >> 批量下載圖片,圖片

批量下載圖片,圖片

編輯:C#入門知識

批量下載圖片,圖片


2. 主要代碼:

private async void button_Click(object sender, RoutedEventArgs e)
        {
            var htmlContent = await FileDownLoader.Instance.GetAsync(webUrl.Text);
            HtmlAgilityPack.HtmlDocument doc = new HtmlAgilityPack.HtmlDocument();
            doc.LoadHtml(htmlContent);
            label1.Content = "";
            var nodeCollection=  doc.DocumentNode.SelectNodes("//img");
            if(nodeCollection!=null)
            {
                string imgDirectory = System.IO.Path.Combine(Environment.CurrentDirectory, "Imgs");
                if(!Directory.Exists(imgDirectory))
                {
                    Directory.CreateDirectory(imgDirectory);
                }
                progressBar.Maximum = nodeCollection.Count;
                progressBar.Value = 0;
                int errorCount = 0;
                foreach (var item in nodeCollection)
                {
                    progressBar.Value += 1;
                    try
                    {
                        var imgSrc=item.GetAttributeValue("src", null);
                        MyImg img = new MyImg(imgSrc);
                        if(!string.IsNullOrEmpty(img.FileName))
                        {
                          await  FileDownLoader.Instance.DownLoadImg(img, imgDirectory);
                        }     
                    }
                    catch (Exception ex)
                    {
                        errorCount += 1;
                    }
                    label1.Content = "第"+progressBar.Value+"個圖片,共"+ progressBar.Maximum+"個圖片,"+errorCount+"個錯誤";
                }
                progressBar.Value = progressBar.Maximum;

            }

  

  public  class FileDownLoader
    {
        HttpClient httpClient = new HttpClient();
        public static FileDownLoader Instance = new FileDownLoader();
        public async Task DownLoadImg(MyImg img,string imgDirectory)
        {
            var imgData = await httpClient.GetByteArrayAsync(img.ImgSrc);
            string newfilename = System.IO.Path.Combine(imgDirectory, img.FileName);
            using (var stream = File.Open(newfilename, FileMode.Create))
            {
                await stream.WriteAsync(imgData, 0, imgData.Length);
            };
        }
        public async Task<string> GetAsync(string url)
        {
         var response= await  httpClient.GetAsync(url);
            return await response.Content.ReadAsStringAsync();
        }
    }

 

    public class MyImg
    {
        public MyImg(string imgSrc)
        {
            if (imgSrc != null)
            {
                var startIndex = imgSrc.LastIndexOf(@"/");
                var startIndex1 = imgSrc.LastIndexOf(".");
                FileName = imgSrc.Substring(startIndex + 1, startIndex1 - startIndex + 3);
                ImgSrc = imgSrc;
            }
        }
        public string FileName { get; set; }

        public string ImgSrc { get; set; }
    }

  

 

  1. 上一頁:
  2. 下一頁:
Copyright © 程式師世界 All Rights Reserved