Download pictures in bulk

Source: Internet
Author: User

2. Main code:

Private async void Button_Click (object sender, RoutedEventArgs e) {var htmlcontent = await filedownload Er.            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.currentdirec                Tory, "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+" photos, total "+ progressbar.maximum+" pictures, "+errorcount+" error ";            } 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;}    }

  

Download pictures in bulk

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.