C#图片批量下载Demo

目录

效果

项目

代码

下载


效果

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<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;

}

}

}

复制代码
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;
        }
    }
}

下载

源码下载

相关推荐
羽凌寒3 分钟前
曝光融合(Exposure Fusion)
图像处理·人工智能·计算机视觉
立秋67894 分钟前
用Python绘制梦幻星空
开发语言·python·pygame
alpszero16 分钟前
YOLO11解决方案之对象裁剪探索
人工智能·python·计算机视觉·yolo11
明月看潮生24 分钟前
青少年编程与数学 02-019 Rust 编程基础 16课题、包、单元包及模块
开发语言·青少年编程·rust·编程与数学
后青春期的诗go30 分钟前
基于Rust语言的Rocket框架和Sqlx库开发WebAPI项目记录(二)
开发语言·后端·rust·rocket框架
Matlab仿真实验室39 分钟前
基于Matlab实现图像透明叠加程序
人工智能·计算机视觉·matlab
草莓熊Lotso1 小时前
【C语言字符函数和字符串函数(一)】--字符分类函数,字符转换函数,strlen,strcpy,strcat函数的使用和模拟实现
c语言·开发语言·经验分享·笔记·其他
盛夏绽放1 小时前
Python字符串常用内置函数详解
服务器·开发语言·python
我想睡觉2611 小时前
Python训练营打卡DAY27
开发语言·python·机器学习
yngsqq1 小时前
(for 循环) VS (LINQ) 性能比拼 ——c#
c#·solr·linq