Baidu image Batch Download tool based on C #

Source: Internet
Author: User
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 cross-threading operations, you should use the delegate's
InitializeComponent ();
}

private void Butselect_click (object sender, EventArgs e) {
FolderBrowserDialog dlg = new FolderBrowserDialog ();
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 the 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
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 section" + 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 + Environment.NewLine);
The path to save is: DestDir;
try {
Downloadimage (Objurl);//Avoid the code in one method too complex
} catch (Exception ex) {
In the code of the child thread, manipulate the interface control to use the BeginInvoke
Textshow.begininvoke (new Action () = {
Textshow.appendtext (ex. Message + Environment.NewLine);
}));
}
}
}
}
private void Downloadimage (string objurl) {
Get the Saved path
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 from Baidu Pictures issued
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);
}
}
}
}
}
  • Related Article

    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.