目录
效果
C#图片批量下载
项目
代码
using Aspose.Cells;
using NLog;
using System;
using System.Collections.Generic;
using System.Data;
using System.Diagnostics;
using System.Drawing;
using System.IO;
using System.Linq;
using System.Net;
using System.Threading;
using System.Threading.Tasks;
using System.Windows.Forms;
namespace DownloadDemo
{
public partial class frmMain : Form
{
public frmMain()
{
InitializeComponent();
NLog.Windows.Forms.RichTextBoxTarget.ReInitializeAllTextboxes(this);
}
String startupPath;
private string excelFileFilter = "表格|*.xlsx;*.xls;";
private Logger log = NLog.LogManager.GetCurrentClassLogger();
CancellationTokenSource cts;
List
bool saveImg = false;
private void frmMain_Load(object sender, EventArgs e)
{
startupPath = System.Windows.Forms.Application.StartupPath;
ServicePointManager.Expect100Continue = false;
ServicePointManager.DefaultConnectionLimit = 512;
}
///
/// 选择表格
///
///
///
private void button2_Click(object sender, EventArgs e)
{
try
{
OpenFileDialog ofd = new OpenFileDialog();
ofd.Filter = excelFileFilter;
if (ofd.ShowDialog() != DialogResult.OK) return;
string excelPath = ofd.FileName;
Workbook workbook = new Workbook(excelPath);
Cells cells = workbook.Worksheets[0].Cells;
System.Data.DataTable dataTable1 = cells.ExportDataTable(1, 0, cells.MaxDataRow, cells.MaxColumn + 1);//noneTitle
//遍历
ltImgInfo.Clear();
ImgInfo temp;
int imgCount = 0;
foreach (DataRow row in dataTable1.Rows)
{
temp = new ImgInfo();
temp.id = row[0].ToString();
temp.title = row[1].ToString();
List
for (int i = 2; i < cells.MaxColumn + 1; i++)
{
string tempStr = row[i].ToString();
if (!string.IsNullOrEmpty(tempStr))
{
list.Add(tempStr);
}
}
temp.images = list;
imgCount = imgCount + list.Count();
ltImgInfo.Add(temp);
}
log.Info("解析完毕,一共[" + ltImgInfo.Count + "]条记录,[" + imgCount + "]张图片!");
}
catch (Exception ex)
{
log.Error("解析表格异常:" + ex.Message);
MessageBox.Show("解析表格异常:" + ex.Message);
}
}
void ShowCostTime(string total, string ocrNum, long time)
{
txtTotal.Invoke(new Action(() =>
{
TimeSpan ts = TimeSpan.FromMilliseconds(time);
txtTotal.Text = string.Format("完成:{0}/{1},用时:{2}"
, ocrNum
, total
, ts.ToString()
);
}));
}
///
/// 下载
///
///
///
private void button1_Click(object sender, EventArgs e)
{
if (ltImgInfo.Count == 0)
{
MessageBox.Show("请先选择表格!");
return;
}
if (!Directory.Exists("img"))
{
Directory.CreateDirectory("img");
}
if (!Directory.Exists("result"))
{
Directory.CreateDirectory("result");
}
btnStop.Enabled = true;
chkSaveImg.Enabled = false;
if (chkSaveImg.Checked)
{
saveImg = true;
}
else
{
saveImg = false;
}
Application.DoEvents();
cts = new CancellationTokenSource();
Task.Factory.StartNew(() =>
{
int totalCount = ltImgInfo.Count();
Stopwatch total = new Stopwatch();
total.Start(); //开始计时
for (int i = 0; i < ltImgInfo.Count(); i++)
{
//判断是否被取消;
if (cts.Token.IsCancellationRequested)
{
return;
}
Stopwatch perID = new Stopwatch();
perID.Start();//开始计时
int imagesCount = ltImgInfo[i].images.Count();
for (int j = 0; j < imagesCount; j++)
{
try
{
Stopwatch sw = new Stopwatch();
sw.Start(); //开始计时
HttpWebRequest request = WebRequest.Create(ltImgInfo[i].images[j]) as HttpWebRequest;
request.KeepAlive = false;
request.ServicePoint.Expect100Continue = false;
request.Timeout = 1000;// 1秒
request.ReadWriteTimeout = 1000;//1秒
request.ServicePoint.UseNagleAlgorithm = false;
request.ServicePoint.ConnectionLimit = 65500;
request.AllowWriteStreamBuffering = false;
request.Proxy = null;
request.CookieContainer = new CookieContainer();
request.CookieContainer.Add(new Cookie("AspxAutoDetectCookieSupport", "1") { Domain = new Uri(ltImgInfo[i].images[j]).Host });
HttpWebResponse wresp = (HttpWebResponse)request.GetResponse();
Stream s = wresp.GetResponseStream();
Bitmap bmp = (Bitmap)System.Drawing.Image.FromStream(s);
s.Dispose();
wresp.Close();
wresp.Dispose();
request.Abort();
sw.Stop();
log.Info(" " + j + "-->下载用时:" + sw.ElapsedMilliseconds + "毫秒");
sw.Restart();
if (saveImg)
{
bmp.Save("img//" + i + "_" + j + ".jpg");
}
}
catch (Exception ex)
{
log.Error(i + "/" + totalCount + "---->id:" + ltImgInfo[i].id + ",url[" + ltImgInfo[i].images[j] + "],异常:" + ex.Message);
}
}
perID.Stop();
log.Info(i + "/" + totalCount + "---->id:" + ltImgInfo[i].id + ",图片张数[" + imagesCount + "],小计用时:" + perID.ElapsedMilliseconds + "毫秒");
ShowCostTime(totalCount.ToString(), i.ToString(), total.ElapsedMilliseconds);
}
total.Stop();
log.Info("全部[" + totalCount + "]共计用时:" + total.ElapsedMilliseconds + "毫秒");
}, TaskCreationOptions.LongRunning);
}
///
/// 停止
///
///
///
private void button3_Click(object sender, EventArgs e)
{
cts.Cancel();
btnStop.Enabled = false;
btnStart.Enabled = true;
chkSaveImg.Enabled = true;
}
}
}
- using Aspose.Cells;
- using NLog;
- using System;
- using System.Collections.Generic;
- using System.Data;
- using System.Diagnostics;
- using System.Drawing;
- using System.IO;
- using System.Linq;
- using System.Net;
- using System.Threading;
- using System.Threading.Tasks;
- using System.Windows.Forms;
-
- namespace DownloadDemo
- {
- public partial class frmMain : Form
- {
- public frmMain()
- {
- InitializeComponent();
- NLog.Windows.Forms.RichTextBoxTarget.ReInitializeAllTextboxes(this);
- }
-
- String startupPath;
- private string excelFileFilter = "表格|*.xlsx;*.xls;";
- private Logger log = NLog.LogManager.GetCurrentClassLogger();
- CancellationTokenSource cts;
- List<ImgInfo> ltImgInfo = new List<ImgInfo>();
-
- bool saveImg = false;
-
- private void frmMain_Load(object sender, EventArgs e)
- {
- startupPath = System.Windows.Forms.Application.StartupPath;
-
- ServicePointManager.Expect100Continue = false;
- ServicePointManager.DefaultConnectionLimit = 512;
- }
-
- /// <summary>
- /// 选择表格
- /// </summary>
- /// <param name="sender"></param>
- /// <param name="e"></param>
- private void button2_Click(object sender, EventArgs e)
- {
- try
- {
- OpenFileDialog ofd = new OpenFileDialog();
- ofd.Filter = excelFileFilter;
- if (ofd.ShowDialog() != DialogResult.OK) return;
- string excelPath = ofd.FileName;
-
- Workbook workbook = new Workbook(excelPath);
- Cells cells = workbook.Worksheets[0].Cells;
- System.Data.DataTable dataTable1 = cells.ExportDataTable(1, 0, cells.MaxDataRow, cells.MaxColumn + 1);//noneTitle
-
- //遍历
- ltImgInfo.Clear();
- ImgInfo temp;
- int imgCount = 0;
- foreach (DataRow row in dataTable1.Rows)
- {
- temp = new ImgInfo();
- temp.id = row[0].ToString();
- temp.title = row[1].ToString();
-
- List<string> list = new List<string>();
- for (int i = 2; i < cells.MaxColumn + 1; i++)
- {
- string tempStr = row[i].ToString();
- if (!string.IsNullOrEmpty(tempStr))
- {
- list.Add(tempStr);
- }
- }
- temp.images = list;
- imgCount = imgCount + list.Count();
- ltImgInfo.Add(temp);
- }
- log.Info("解析完毕,一共[" + ltImgInfo.Count + "]条记录,[" + imgCount + "]张图片!");
- }
- catch (Exception ex)
- {
- log.Error("解析表格异常:" + ex.Message);
- MessageBox.Show("解析表格异常:" + ex.Message);
- }
- }
-
- void ShowCostTime(string total, string ocrNum, long time)
- {
- txtTotal.Invoke(new Action(() =>
- {
- TimeSpan ts = TimeSpan.FromMilliseconds(time);
- txtTotal.Text = string.Format("完成:{0}/{1},用时:{2}"
- , ocrNum
- , total
- , ts.ToString()
- );
- }));
- }
-
- /// <summary>
- /// 下载识别
- /// </summary>
- /// <param name="sender"></param>
- /// <param name="e"></param>
- private void button1_Click(object sender, EventArgs e)
- {
- if (ltImgInfo.Count == 0)
- {
- MessageBox.Show("请先选择表格!");
- return;
- }
-
- if (!Directory.Exists("img"))
- {
- Directory.CreateDirectory("img");
- }
-
- if (!Directory.Exists("result"))
- {
- Directory.CreateDirectory("result");
- }
-
- btnStop.Enabled = true;
- chkSaveImg.Enabled = false;
-
- if (chkSaveImg.Checked)
- {
- saveImg = true;
- }
- else
- {
- saveImg = false;
- }
-
- Application.DoEvents();
-
- cts = new CancellationTokenSource();
- Task.Factory.StartNew(() =>
- {
-
- int totalCount = ltImgInfo.Count();
- Stopwatch total = new Stopwatch();
- total.Start(); //开始计时
- for (int i = 0; i < ltImgInfo.Count(); i++)
- {
-
- //判断是否被取消;
- if (cts.Token.IsCancellationRequested)
- {
- return;
- }
-
- Stopwatch perID = new Stopwatch();
- perID.Start();//开始计时
- int imagesCount = ltImgInfo[i].images.Count();
- for (int j = 0; j < imagesCount; j++)
- {
- try
- {
- Stopwatch sw = new Stopwatch();
- sw.Start(); //开始计时
- HttpWebRequest request = WebRequest.Create(ltImgInfo[i].images[j]) as HttpWebRequest;
- request.KeepAlive = false;
- request.ServicePoint.Expect100Continue = false;
- request.Timeout = 1000;// 1秒
- request.ReadWriteTimeout = 1000;//1秒
-
- request.ServicePoint.UseNagleAlgorithm = false;
- request.ServicePoint.ConnectionLimit = 65500;
- request.AllowWriteStreamBuffering = false;
- request.Proxy = null;
-
- request.CookieContainer = new CookieContainer();
- request.CookieContainer.Add(new Cookie("AspxAutoDetectCookieSupport", "1") { Domain = new Uri(ltImgInfo[i].images[j]).Host });
-
- HttpWebResponse wresp = (HttpWebResponse)request.GetResponse();
- Stream s = wresp.GetResponseStream();
- Bitmap bmp = (Bitmap)System.Drawing.Image.FromStream(s);
- s.Dispose();
- wresp.Close();
- wresp.Dispose();
- request.Abort();
-
- sw.Stop();
- log.Info(" " + j + "-->下载用时:" + sw.ElapsedMilliseconds + "毫秒");
- sw.Restart();
-
- if (saveImg)
- {
- bmp.Save("img//" + i + "_" + j + ".jpg");
- }
-
- }
- catch (Exception ex)
- {
- log.Error(i + "/" + totalCount + "---->id:" + ltImgInfo[i].id + ",url[" + ltImgInfo[i].images[j] + "],异常:" + ex.Message);
- }
- }
- perID.Stop();
- log.Info(i + "/" + totalCount + "---->id:" + ltImgInfo[i].id + ",图片张数[" + imagesCount + "],小计用时:" + perID.ElapsedMilliseconds + "毫秒");
-
- ShowCostTime(totalCount.ToString(), i.ToString(), total.ElapsedMilliseconds);
- }
- total.Stop();
- log.Info("全部[" + totalCount + "]共计用时:" + total.ElapsedMilliseconds + "毫秒");
- }, TaskCreationOptions.LongRunning);
- }
-
- /// <summary>
- /// 停止
- /// </summary>
- /// <param name="sender"></param>
- /// <param name="e"></param>
- private void button3_Click(object sender, EventArgs e)
- {
- cts.Cancel();
- btnStop.Enabled = false;
- btnStart.Enabled = true;
-
- chkSaveImg.Enabled = true;
- }
- }
- }
下载


评论记录:
回复评论: