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 图片下载器 {
public partial class Form1 : Form {
private string dir;
public Form1() {
Control.CheckForIllegalCrossThreadCalls = false;//这种方法不推荐使用,即不检查跨线程操作,应该使用委托的
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("目前正在下载第" + pagecount + "页\n");
}
private void butStart_Click(object sender , EventArgs e) {
string key = textKeyWords.Text;
if (string.IsNullOrEmpty(key)) {//检测关键字
MessageBox.Show("请输入关键词!");
return;
}
if (string.IsNullOrEmpty(textDir.Text)) {//检测路径
MessageBox.Show("请选择路径!");
return;
}
dir = textDir.Text;
if (!dir.EndsWith("\\")) {
dir = dir + "\\";
}
Thread thread = new Thread(() => {//启动一个新线程
process(key);
});
thread.Start();//线程启动
}
private void process(string key) {
int count = (int)numericUpDown.Value;//请求的页面数量
for (int i = 0 ; 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) * 60 + "&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("获取第" + i + "页失败!" + 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 JObject img = (JObject)imgs[j];
string objUrl = (string)img["objURL"];//http://hibiadu....../1.jpg
// textShow.AppendText(objUrl + Environment.NewLine);
//保存的路径是:destDir;
try {
DownloadImage(objUrl);//避免一个方法中的代码过于复杂
} catch (Exception ex) {
//子线程的代码中操作界面控件要使用BeginInvoke
textShow.BeginInvoke(new Action(() => {
textShow.AppendText(ex.Message + Environment.NewLine);
}));
}
}
}
}
private void DownloadImage(string objUrl) {
//得到保存的路径
string path = Path.Combine(dir , Path.GetFileName(objUrl));
HttpWebRequest req = (HttpWebRequest)WebRequest.Create(objUrl);
req.Referer = "http://image.baidu.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)) {
stream.CopyTo(filestream);
}
} else {
throw new Exception("下载失败" + res.StatusCode);
}
}
}
}
}
0
0
相关文章
c# Thread.ApartmentState (STA/MTA) 和并发的关系
Blazor 项目发布和部署到 IIS 的步骤
c# C# 12 的 Interceptors 和并发代码的AOP实现
EF Core如何反向工程现有数据库 EF Core Database First详细步骤
c# System.IO.Pipelines 是什么 如何提升IO性能
本站声明:本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系admin@php.cn
热门AI工具
相关专题
C++ 高级模板编程与元编程
本专题深入讲解 C++ 中的高级模板编程与元编程技术,涵盖模板特化、SFINAE、模板递归、类型萃取、编译时常量与计算、C++17 的折叠表达式与变长模板参数等。通过多个实际示例,帮助开发者掌握 如何利用 C++ 模板机制编写高效、可扩展的通用代码,并提升代码的灵活性与性能。
10
2026.01.23
Golang 性能分析与pprof调优实战
本专题系统讲解 Golang 应用的性能分析与调优方法,重点覆盖 pprof 的使用方式,包括 CPU、内存、阻塞与 goroutine 分析,火焰图解读,常见性能瓶颈定位思路,以及在真实项目中进行针对性优化的实践技巧。通过案例讲解,帮助开发者掌握 用数据驱动的方式持续提升 Go 程序性能与稳定性。
9
2026.01.22
热门下载
精品课程
相关推荐
/
热门推荐
/
最新课程
最新文章







