实例介绍
【实例简介】批量下载图片
首先从文本文件导入要下载的图片网址(每行一个即可),然后点击开始 即可批量下载,图片下载至bin目录下的【路径】文件夹了
【实例截图】
【核心代码】
using System; using System.Collections; using System.ComponentModel; using System.Drawing; using System.IO; using System.Net; using System.Windows.Forms; namespace imagesclq { public class frmimages : Form { private int zgo = 0; private int cg = 0; private int sb = 0; private ArrayList sbd = new ArrayList(); private ArrayList listdb = new ArrayList(); private IContainer components = null; private Button button1; private Button button2; private FolderBrowserDialog fldefie; private Label lbllu; private Button button3; private Label lbljianlu; private OpenFileDialog ofiledbs; private TextBox txtshijia; private Timer timer1; private Label label1; private Label label2; private TextBox txtnancom; private Label lbltup; private Button button4; private CheckBox cboissf; private TabControl tabControl1; private TabPage tabPage1; private TabPage tabPage2; private Label lblsb; private Label lblcg; private Label lblzg; private TextBox txtzt; private RichTextBox rtbsbd; private Button button5; public frmimages() { this.InitializeComponent(); } private void button1_Click(object sender, EventArgs e) { this.timer1.Enabled = true; this.timer1.Start(); if (this.txtshijia.Text != "") { this.timer1.Interval = Convert.ToInt32(this.txtshijia.Text); } if (this.txtnancom.Text == "") { MessageBox.Show("请选择后缀"); this.timer1.Stop(); } } private void geturl() { try { if (this.zgo >= this.listdb.Count) { this.timer1.Stop(); if (this.cboissf.Checked) { Application.Exit(); } MessageBox.Show("下载完毕"); this.listdb.Clear(); } else { string url = this.listdb[this.zgo].ToString(); this.txtzt.Text = this.listdb[this.zgo].ToString(); string wjian = url.Substring(url.IndexOf(this.txtnancom.Text) this.txtnancom.Text.Length); string lujian = wjian.Remove(wjian.LastIndexOf("/")).Replace("/", "\\"); string zgliu = this.lbllu.Text "/" lujian; string filenames = url.Substring(url.LastIndexOf("/")); Directory.CreateDirectory(zgliu); WebClient mywebclient = new WebClient(); mywebclient.DownloadFile(url, zgliu filenames); this.zgo ; this.cg ; this.lblcg.Text = "成功:" this.cg.ToString() "个"; } } catch (Exception) { this.sb ; this.sbd.Add(this.txtzt.Text); RichTextBox richTextBox = this.rtbsbd; richTextBox.Text = richTextBox.Text this.txtzt.Text "\r\n"; this.lblsb.Text = "失败:" this.sb.ToString() "个"; this.zgo ; } } private void button2_Click(object sender, EventArgs e) { if (DialogResult.OK == this.fldefie.ShowDialog()) { this.lbllu.Text = this.fldefie.SelectedPath; } } private void button3_Click(object sender, EventArgs e) { if (DialogResult.OK == this.ofiledbs.ShowDialog()) { this.lbljianlu.Text = this.ofiledbs.FileName; try { FileStream aFile = new FileStream(this.lbljianlu.Text, FileMode.Open); StreamReader sr = new StreamReader(aFile); string stlianj2 = ""; while ((stlianj2 = sr.ReadLine()) != null) { this.listdb.Add(stlianj2); } MessageBox.Show("导入链接成功"); this.lblzg.Text = "总共:" this.listdb.Count.ToString(); sr.Close(); aFile.Close(); } catch (Exception) { MessageBox.Show("导入链接失败"); } } } private void txtshijia_TextChanged(object sender, EventArgs e) { } private void timer1_Tick(object sender, EventArgs e) { this.geturl(); } private void fldefie_HelpRequest(object sender, EventArgs e) { } private void frmimages_Load(object sender, EventArgs e) { string ds = DateTime.Now.ToString("yyyyMMdd"); double dd = Convert.ToDouble(ds); //if (dd > 20150801.0) //{ // MessageBox.Show("软件过期了"); // Application.Exit(); //} MessageBox.Show("欢迎使用!"); } private void button4_Click(object sender, EventArgs e) { Application.Exit(); } private void button5_Click(object sender, EventArgs e) { this.zgo = 0; this.cg = 0; this.sb = 0; this.listdb.Clear(); if (this.sbd.Count > 0) { for (int i = 0; i < this.sbd.Count; i ) { this.listdb.Add(this.sbd[i].ToString()); } } this.timer1.Enabled = true; this.timer1.Start(); this.sbd.Clear(); } protected override void Dispose(bool disposing) { if (disposing && this.components != null) { this.components.Dispose(); } base.Dispose(disposing); } private void InitializeComponent() { this.components = new System.ComponentModel.Container(); this.button1 = new System.Windows.Forms.Button(); this.button2 = new System.Windows.Forms.Button(); this.fldefie = new System.Windows.Forms.FolderBrowserDialog(); this.lbllu = new System.Windows.Forms.Label(); this.button3 = new System.Windows.Forms.Button(); this.lbljianlu = new System.Windows.Forms.Label(); this.ofiledbs = new System.Windows.Forms.OpenFileDialog(); this.txtshijia = new System.Windows.Forms.TextBox(); this.timer1 = new System.Windows.Forms.Timer(this.components); this.label1 = new System.Windows.Forms.Label(); this.label2 = new System.Windows.Forms.Label(); this.txtnancom = new System.Windows.Forms.TextBox(); this.lbltup = new System.Windows.Forms.Label(); this.button4 = new System.Windows.Forms.Button(); this.cboissf = new System.Windows.Forms.CheckBox(); this.tabControl1 = new System.Windows.Forms.TabControl(); this.tabPage1 = new System.Windows.Forms.TabPage(); this.lblzg = new System.Windows.Forms.Label(); this.lblsb = new System.Windows.Forms.Label(); this.lblcg = new System.Windows.Forms.Label(); this.tabPage2 = new System.Windows.Forms.TabPage(); this.rtbsbd = new System.Windows.Forms.RichTextBox(); this.txtzt = new System.Windows.Forms.TextBox(); this.button5 = new System.Windows.Forms.Button(); this.tabControl1.SuspendLayout(); this.tabPage1.SuspendLayout(); this.tabPage2.SuspendLayout(); this.SuspendLayout(); // // button1 // this.button1.Location = new System.Drawing.Point(12, 91); this.button1.Name = "button1"; this.button1.Size = new System.Drawing.Size(136, 83); this.button1.TabIndex = 0; this.button1.Text = "开始"; this.button1.UseVisualStyleBackColor = true; // // button2 // this.button2.Location = new System.Drawing.Point(21, 370); this.button2.Name = "button2"; this.button2.Size = new System.Drawing.Size(75, 65); this.button2.TabIndex = 1; this.button2.Text = "保存路径"; this.button2.UseVisualStyleBackColor = true; // // lbllu // this.lbllu.AutoSize = true; this.lbllu.Location = new System.Drawing.Point(119, 410); this.lbllu.Name = "lbllu"; this.lbllu.Size = new System.Drawing.Size(29, 12); this.lbllu.TabIndex = 2; this.lbllu.Text = "路径"; // // button3 // this.button3.Location = new System.Drawing.Point(21, 300); this.button3.Name = "button3"; this.button3.Size = new System.Drawing.Size(75, 44); this.button3.TabIndex = 3; this.button3.Text = "导入文件"; this.button3.UseVisualStyleBackColor = true; // // lbljianlu // this.lbljianlu.AutoSize = true; this.lbljianlu.Location = new System.Drawing.Point(115, 316); this.lbljianlu.Name = "lbljianlu"; this.lbljianlu.Size = new System.Drawing.Size(77, 12); this.lbljianlu.TabIndex = 4; this.lbljianlu.Text = "图片链接路径"; // // ofiledbs // this.ofiledbs.FileName = "openFileDialog1"; // // txtshijia // this.txtshijia.Location = new System.Drawing.Point(65, 62); this.txtshijia.Name = "txtshijia"; this.txtshijia.Size = new System.Drawing.Size(64, 21); this.txtshijia.TabIndex = 5; this.txtshijia.Text = "1"; // // timer1 // this.timer1.Interval = 4500; // // label1 // this.label1.AutoSize = true; this.label1.Location = new System.Drawing.Point(19, 64); this.label1.Name = "label1"; this.label1.Size = new System.Drawing.Size(41, 12); this.label1.TabIndex = 6; this.label1.Text = "线程:"; // // label2 // this.label2.AutoSize = true; this.label2.Location = new System.Drawing.Point(135, 65); this.label2.Name = "label2"; this.label2.Size = new System.Drawing.Size(83, 12); this.label2.TabIndex = 7; this.label2.Text = "网址后缀.com:"; // // txtnancom // this.txtnancom.Location = new System.Drawing.Point(224, 59); this.txtnancom.Name = "txtnancom"; this.txtnancom.Size = new System.Drawing.Size(70, 21); this.txtnancom.TabIndex = 8; this.txtnancom.Text = "com"; // // lbltup // this.lbltup.AutoSize = true; this.lbltup.Location = new System.Drawing.Point(19, 20); this.lbltup.Name = "lbltup"; this.lbltup.Size = new System.Drawing.Size(95, 12); this.lbltup.TabIndex = 9; this.lbltup.Text = "正在下载的图片:"; // // button4 // this.button4.Location = new System.Drawing.Point(21, 212); this.button4.Name = "button4"; this.button4.Size = new System.Drawing.Size(75, 66); this.button4.TabIndex = 10; this.button4.Text = "退出"; this.button4.UseVisualStyleBackColor = true; // // cboissf // this.cboissf.AutoSize = true; this.cboissf.Location = new System.Drawing.Point(21, 190); this.cboissf.Name = "cboissf"; this.cboissf.Size = new System.Drawing.Size(96, 16); this.cboissf.TabIndex = 11; this.cboissf.Text = "是否发完退出"; this.cboissf.UseVisualStyleBackColor = true; // // tabControl1 // this.tabControl1.Controls.Add(this.tabPage1); this.tabControl1.Controls.Add(this.tabPage2); this.tabControl1.Location = new System.Drawing.Point(171, 91); this.tabControl1.Name = "tabControl1"; this.tabControl1.SelectedIndex = 0; this.tabControl1.Size = new System.Drawing.Size(541, 187); this.tabControl1.TabIndex = 12; // // tabPage1 // this.tabPage1.Controls.Add(this.lblzg); this.tabPage1.Controls.Add(this.lblsb); this.tabPage1.Controls.Add(this.lblcg); this.tabPage1.Location = new System.Drawing.Point(4, 22); this.tabPage1.Name = "tabPage1"; this.tabPage1.Padding = new System.Windows.Forms.Padding(3); this.tabPage1.Size = new System.Drawing.Size(533, 161); this.tabPage1.TabIndex = 0; this.tabPage1.Text = "状态"; this.tabPage1.UseVisualStyleBackColor = true; // // lblzg // this.lblzg.AutoSize = true; this.lblzg.Location = new System.Drawing.Point(22, 33); this.lblzg.Name = "lblzg"; this.lblzg.Size = new System.Drawing.Size(53, 12); this.lblzg.TabIndex = 2; this.lblzg.Text = "总共多少"; // // lblsb // this.lblsb.AutoSize = true; this.lblsb.Location = new System.Drawing.Point(22, 116); this.lblsb.Name = "lblsb"; this.lblsb.Size = new System.Drawing.Size(29, 12); this.lblsb.TabIndex = 1; this.lblsb.Text = "失败"; // // lblcg // this.lblcg.AutoSize = true; this.lblcg.Location = new System.Drawing.Point(22, 80); this.lblcg.Name = "lblcg"; this.lblcg.Size = new System.Drawing.Size(29, 12); this.lblcg.TabIndex = 0; this.lblcg.Text = "成功"; // // tabPage2 // this.tabPage2.Controls.Add(this.rtbsbd); this.tabPage2.Location = new System.Drawing.Point(4, 22); this.tabPage2.Name = "tabPage2"; this.tabPage2.Padding = new System.Windows.Forms.Padding(3); this.tabPage2.Size = new System.Drawing.Size(533, 161); this.tabPage2.TabIndex = 1; this.tabPage2.Text = "失败的"; this.tabPage2.UseVisualStyleBackColor = true; // // rtbsbd // this.rtbsbd.Location = new System.Drawing.Point(6, 10); this.rtbsbd.Name = "rtbsbd"; this.rtbsbd.Size = new System.Drawing.Size(521, 128); this.rtbsbd.TabIndex = 0; this.rtbsbd.Text = ""; // // txtzt // this.txtzt.Location = new System.Drawing.Point(118, 17); this.txtzt.Name = "txtzt"; this.txtzt.Size = new System.Drawing.Size(628, 21); this.txtzt.TabIndex = 13; // // button5 // this.button5.Location = new System.Drawing.Point(302, 84); this.button5.Name = "button5"; this.button5.Size = new System.Drawing.Size(388, 23); this.button5.TabIndex = 14; this.button5.Text = "清空历史采集失败的"; this.button5.UseVisualStyleBackColor = true; // // frmimages // this.AutoScaleDimensions = new System.Drawing.SizeF(6F, 12F); this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font; this.ClientSize = new System.Drawing.Size(758, 449); this.Controls.Add(this.button5); this.Controls.Add(this.txtzt); this.Controls.Add(this.tabControl1); this.Controls.Add(this.cboissf); this.Controls.Add(this.button4); this.Controls.Add(this.lbltup); this.Controls.Add(this.txtnancom); this.Controls.Add(this.label2); this.Controls.Add(this.label1); this.Controls.Add(this.txtshijia); this.Controls.Add(this.lbljianlu); this.Controls.Add(this.button3); this.Controls.Add(this.lbllu); this.Controls.Add(this.button2); this.Controls.Add(this.button1); this.MaximizeBox = false; this.Name = "frmimages"; this.StartPosition = System.Windows.Forms.FormStartPosition.WindowsDefaultBounds; this.Text = "8.8星辰图片批量下载:QQ 1749408006"; this.tabControl1.ResumeLayout(false); this.tabPage1.ResumeLayout(false); this.tabPage1.PerformLayout(); this.tabPage2.ResumeLayout(false); this.ResumeLayout(false); this.PerformLayout(); } } }
网友评论
小贴士
感谢您为本站写下的评论,您的评论对其它用户来说具有重要的参考价值,所以请认真填写。
- 类似“顶”、“沙发”之类没有营养的文字,对勤劳贡献的楼主来说是令人沮丧的反馈信息。
- 相信您也不想看到一排文字/表情墙,所以请不要反馈意义不大的重复字符,也请尽量不要纯表情的回复。
- 提问之前请再仔细看一遍楼主的说明,或许是您遗漏了。
- 请勿到处挖坑绊人、招贴广告。既占空间让人厌烦,又没人会搭理,于人于己都无利。
关于好例子网
本站旨在为广大IT学习爱好者提供一个非营利性互相学习交流分享平台。本站所有资源都可以被免费获取学习研究。本站资源来自网友分享,对搜索内容的合法性不具有预见性、识别性、控制性,仅供学习研究,请务必在下载后24小时内给予删除,不得用于其他任何用途,否则后果自负。基于互联网的特殊性,平台无法对用户传输的作品、信息、内容的权属或合法性、安全性、合规性、真实性、科学性、完整权、有效性等进行实质审查;无论平台是否已进行审查,用户均应自行承担因其传输的作品、信息、内容而可能或已经产生的侵权或权属纠纷等法律责任。本站所有资源不代表本站的观点或立场,基于网友分享,根据中国法律《信息网络传播权保护条例》第二十二与二十三条之规定,若资源存在侵权或相关问题请联系本站客服人员,点此联系我们。关于更多版权及免责申明参见 版权及免责申明
支持(0) 盖楼(回复)