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

下载

源码下载

相关推荐
熊思宇1 小时前
MoonSharp 文档二
c#·moonsharp
MY-备忘1 小时前
用友U9二次开发-问题记录
c#·用友
PfCoder1 小时前
C#的判断语句总结
开发语言·c#·visual studio·winform
好看资源平台2 小时前
Java/Kotlin逆向基础与Smali语法精解
java·开发语言·kotlin
紫雾凌寒2 小时前
计算机视觉应用|自动驾驶的感知革命:多传感器融合架构的技术演进与落地实践
人工智能·机器学习·计算机视觉·架构·自动驾驶·多传感器融合·waymo
wjcroom2 小时前
数字投屏叫号器-发射端python窗口定制
开发语言·python
静候光阴2 小时前
python使用venv命令创建虚拟环境(ubuntu22)
linux·开发语言·python
Y1nhl2 小时前
力扣hot100_二叉树(4)_python版本
开发语言·pytorch·python·算法·leetcode·机器学习
阿木看源码3 小时前
bindingAdapter的异常错误
java·开发语言
学习两年半的Javaer3 小时前
Rust语言基础知识详解【九】
开发语言·rust