using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.IO
using System.Drawing;
using System.IO .Text;
using System.Threading;
using System.Threading.Tasks;
using System.Windows.Forms;
using Newtonsoft public partial class Form1 : Form {
private string dir;
public Form1() { ,即不要檢查跨執行緒操作,應使用委託的
InitializeComponent();
}
private void butSelect_Click(object sender , EventArgs e) {
if (dlg.ShowDialog() == System.Windows.Forms.DialogResult.OK) {
;
}
}
oid Showpages() {
this.textShow.AppendText("目前下載第" + pagecount + "頁n");
object sender , EventArgs e) {
string key = textKeyWords.Text;
MessageBox.Show("請輸入關鍵字! ");
return;
{//偵測路徑
}
dir = textDir.Text;
if (!dir.EndsWith("\")) {
Thread thread = new Thread(() => {//啟動新執行緒
}); int count = (int)numericUpDown.Value;//要求的頁面數量
for (int i = 0 ; i pagecount = i + 1;
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) * 60 + "&rn=60&itg=0&z=0&fr=&寬度=&高度=&lm=-1&ic=0&s=0&st=-1&gsm=360600003c");
using (HttpWebResponse res = (HttpWebResponse)req.GetResponse () == HttpStatusCode.OK) {
嘗試 {
;
} catch (Exception e) {
textShow.BeginInvoke(new Action(() => {
textShow.AppendText(e.Message + Environment.NewLine);
})); }
}
} 其他 {
}
}
}
私人無效下載(串流){ 使用 (StreamReader reader = new Reader(stream)) { 使用 (StreamReader reader ReadToEnd();
JObject objRoot = (JObject)JsonConvert.DeserializeObject(json);
JArray imgs = (JArray)objRoot["imgs"];
for (int j = 0 ; j
string objUrl = (string)img["objURL"];//http://hibia // textShow.AppendText(objUrl + Environment.NewLine);
//保存的路徑為:destDir;
DownloadImage(objUrl);//避免一個方法的程式碼過於複雜
} catch (Except //子執行緒的程式碼中操作介面控制要使用BeginInvoke
textShow.AppendText(ex.Message + Environment.NewLine);
}));
}
}
}
}
private void DownloadImage(string objUrl) {
//得到保存的路徑
string path = Path.Combine (dir , Path.GetFileName(objUrl));
HttpWebRequest req = (HttpWebRequest)WebRequest.Create aidu.com/";//欺騙網站伺服器這是從百度圖片發出的
using (HttpWebResponse res = (HttpWebResponse)req.GetResponse()) {
if (res.StatusCode == HttpStatusCode.OK) {
using (Stream stream = res.GetResponseStream())
using (Stream filestream = new FileStream(path , FileMode.Create)) {
}
} else {
}
}
}