Implementation of batch download tool based on C # Baidu image

Source: Internet
Author: User

don't have a net at home, how to be bored? It's better to look at some beautiful pictures, the network is fast batch download, there is time to look slowly, hey, I am a good person. So the implementation of this tool, it is almost imminent ah, to see how the realization of it.

First piece bar:

This is the WinForm interface of the software, based on the C # implementation.
On the code, there are more than 100 lines.

Using system;using system.collections.generic;using system.componentmodel;using system.data;using System.Drawing; Using system.io;using system.linq;using system.net;using system.text;using system.threading;using system.threading.tasks;using system.windows.forms;using newtonsoft.json;using Newtonsoft.Json.Linq;namespace Picture Downloader        {public partial class Form1:form {private string dir; Public Form1 () {Control.checkforillegalcrossthreadcalls = false;//This method is not recommended, that is, do not check for cross-threading operations, the delegate's Initi should be used        Alizecomponent (); private void Butselect_click (object sender, EventArgs e) {FolderBrowserDialog dlg = new Folderbrowse            Rdialog (); if (dlg. ShowDialog () = = System.Windows.Forms.DialogResult.OK) {Textdir.text = dlg.            SelectedPath;        }} public static int pagecount = 1;        private void Showpages () {this.textShow.AppendText ("Currently downloading section" + pagecount + "page \ n"); } private void butStart_click (object sender, EventArgs e) {string key = Textkeywords.text; if (string. IsNullOrEmpty (key)) {//Detection keyword MessageBox.Show ("Please enter keywords!                ");            Return } if (string. IsNullOrEmpty (Textdir.text)) {//Detection path MessageBox.Show ("Please select path!")                ");            Return            } dir = Textdir.text; if (!dir.            EndsWith ("\ \")) {dir = dir + "\ \";            Thread thread = new Thread (() = {//Start a new threading process (key);            }); Thread. Start ();//thread Start} private void Process (string key) {int count = (int) numericupdown.value;//requested number of pages                Volume for (int i = 0; i < count; i++) {PageCount = i + 1;                Showpages (); HttpWebRequest req = (HttpWebRequest) webrequest.create ("http://image.baidu.com/search/avatarjson?tn= resultjsonavatarnew&ie=utf-8&word= "+ uri.escapeuristRing (Key) + "&cg=girl&pn=" + (i + 1) * + + "&rn=60&itg=0&z=0&fr=&width=&height=&lm=                -1&ic=0&s=0&st=-1&gsm=360600003c "); using (HttpWebResponse res = (HttpWebResponse) req. GetResponse ()) {if (res). StatusCode = = Httpstatuscode.ok) {using (Stream stream = res).                            GetResponseStream ()) {try {download (stream);                                    } catch (Exception e) {textshow.begininvoke (new Action () = {                                Textshow.appendtext (E.message + Environment.NewLine);                            })); }}} else {MessageBox.Show ("get the" + i + "page failed!) "+ Res.                    StatusCode); }}}} private void download (Stream stream) {USINg (StreamReader reader = new StreamReader (stream)) {string json = reader.                ReadToEnd ();                Jobject objroot = (jobject) jsonconvert.deserializeobject (JSON);                Jarray IMGs = (jarray) objroot["IMGs"]; for (int j = 0; J < IMGs. Count;                    J + +) {jobject img = (jobject) imgs[j]; String objurl = (string) img["Objurl"];//http://hibiadu....../1.jpg//Textshow.appendtext (Objurl + Envi Ronment.                    NewLine);                    The saved path is: DestDir; try {downloadimage (objurl);//avoid complex code in one method } catch (Exception ex) {//Sub-threading code in the action interface control to use BeginInvoke Textshow.begininvok E (New Action () = {Textshow.appendtext (ex.                        Message + Environment.NewLine);                    })); }}}} private void Downloadimage (String Objurl) {//Gets the saved path of string path = Path.Combine (dir, Path.getfilename (Objurl));            HttpWebRequest req = (HttpWebRequest) webrequest.create (Objurl); Req. Referer = "http://image.baidu.com/";//spoof Web server This is the image emitted from Baidu using (httpwebresponse res = (HttpWebResponse) req. GetResponse ()) {if (res). StatusCode = = Httpstatuscode.ok) {using (Stream stream = res).                        GetResponseStream ()) using (Stream filestream = new FileStream (path, FileMode.Create)) { Stream.                    CopyTo (FileStream); }} else {throw new Exception ("Download failed" + Res.)                StatusCode); }            }        }    }}

Right-click to view the image to see a larger image.
The end result is this appearance, how, self-feeling is still possible.

Baidu Network disk Download: http://pan.baidu.com/s/1kT3YzXl Password: Gafi
The first for the entire solution of the program, the second for the program compiled after the running program, Windows can run directly.


Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.

Implementation of batch download tool based on C # Baidu image

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.