DownloadWebImages:下载某页面上的所有图片

用C#写了一个下载网络图片的程序,比较简单,原理: 使用WebBrowser控件来打开URL,然后,遍历WebBrowser中的所有的Image即可,同时,另存为的时候,可以从网络下载图片,保存到本地.  

【网通】 点击这里下载全部源程序    【电信、网通】点击这里下载源程序

【下载说明】
1、单击上面这个地址,打开下载页面。
2、点普通下载--等待30秒--点“下载”按钮--保存

主要源程序:

/*
 * Created by SharpDevelop.
 * User: Administrator
 * Date: 2012-6-15
 * Time: 7:47
 * 
 * To change this template use Tools | Options | Coding | Edit Standard Headers.
 */
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Drawing;
using System.IO;
using System.Net;
using System.Windows.Forms;


namespace DownloadWebImages
{
 /// <summary>
 /// Description of MainForm.
 /// </summary>
 public partial class MainForm : Form
 {
  private WebBrowser webBrowser=new WebBrowser();
  
  public MainForm()
  {
   //
   // The InitializeComponent() call is required for Windows Forms designer support.
   //
   InitializeComponent();
   
   //
   // TODO: Add constructor code after the InitializeComponent() call.
   //
   this.listBox1.Items.Clear();
   this.label4.Text="";
   this.label5.Text="";
   this.toolStripStatusLabel1.Text="";
  }
  
  void BtnGoClick(object sender, EventArgs e)
  {
   if(this.textBox1.Text!=string.Empty && this.textBox1.Text!=""){
    if( !this.textBox1.Text.StartsWith("http://")){
     MessageBox.Show("地址必须以http://开头!","提示");
     return;
    }
    
    this.listBox1.Items.Clear();
    
    this.webBrowser.Navigate(this.textBox1.Text);
    this.webBrowser.DocumentCompleted+=new WebBrowserDocumentCompletedEventHandler(WebBrowser_DocumentCompleted);
   }
  }

  void WebBrowser_DocumentCompleted(object sender, WebBrowserDocumentCompletedEventArgs e)
  {
   this.progressBar1.Minimum=0;
   this.progressBar1.Maximum=this.webBrowser.Document.Images.Count;
   this.progressBar1.Value=0;
   
   string img_src=string.Empty;
   foreach(HtmlElement element in this.webBrowser.Document.Images){
    img_src=element.GetAttribute("src");
    
//    Trace.WriteLine(img_src);
    if(img_src.Trim()!=""){
     this.listBox1.Items.Add(img_src);
     this.listBox1.Refresh();
     
     this.label4.Text=this.listBox1.Items.Count.ToString()+" files";
     this.label4.Refresh();
     
     this.progressBar1.Value++;
     
     this.label5.Text=this.progressBar1.Value.ToString()+"/"+this.progressBar1.Maximum.ToString();
     this.label5.Refresh();
    }
   }
   
   DeleteSameImages();
  }
  
  void DeleteSameImages()
  {
  begin:
   for(int j=0;j<this.listBox1.Items.Count-1;j++)
   {
    for(int k=j+1;k<this.listBox1.Items.Count;k++)
    {
     if(this.listBox1.Items[k].ToString()==this.listBox1.Items[j].ToString())
     {
      this.listBox1.Items.RemoveAt(k);
      goto begin;
     }
    }
   }
  }
  
  void ListBox1MouseClick(object sender, MouseEventArgs e)
  {
   this.pictureBox1.ImageLocation=this.listBox1.SelectedItem.ToString();
   this.toolStripStatusLabel1.Text=this.listBox1.SelectedItem.ToString();
  }
  
  void SaveAsToolStripMenuItemClick(object sender, EventArgs e)
  {
   SaveFileDialog sfd=new SaveFileDialog();
   sfd.Filter="JPG File|*.jpg|PNG File|*.png|GIF File|*.gif";
   if(sfd.ShowDialog()==DialogResult.OK)
   {
    this.DownloadImageFile(this.pictureBox1.ImageLocation,sfd.FileName);
   }
  }
  
  void DownloadImageFile(string url,string filePath)
  {
   FileStream fsFileStream = null;
   HttpWebRequest webRequest = null;
   HttpWebResponse webResponse = null;

   try
   {
    byte[] BUFFER = new byte[307200]; // 300 KB
    webRequest = (HttpWebRequest)WebRequest.Create(url);
    webRequest.Method = "GET";
    webRequest.Timeout = 120000;
    webResponse = (HttpWebResponse)webRequest.GetResponse(); //Getting the response

    if (webResponse.StatusCode.ToString().Equals("OK"))
    {
     Stream responseStream = webResponse.GetResponseStream();
     Stream ToStream = null;

     try
     {
      if (File.Exists(filePath))
      {
       File.Delete(filePath);
      }

      ToStream = File.Open(filePath, FileMode.OpenOrCreate, FileAccess.Write);

      int count = 1;

      while (count != 0)
      {
       count = responseStream.Read(BUFFER, 0, BUFFER.Length);
       ToStream.Write(BUFFER, 0, count);
       ToStream.Flush();
      }
     }
     catch (Exception exception)
     {
      MessageBox.Show(exception.Message);
     }
     finally
     {
      ToStream.Close();
      ToStream = null;
     }

     pictureBox1.Image = new Bitmap(filePath);

    }
   }
   catch (Exception exp)
   {
    MessageBox.Show(exp.Message);
   }
   finally
   {
    if (fsFileStream != null)
    {
     //Closing file stream
     fsFileStream.Close();
     fsFileStream = null;
    }
    if (webRequest != null)
    {
     webRequest = null;
    }
    if (webResponse != null)
    {
     webResponse.Close();
     webResponse = null;
    }
   }
  }
  
  void ListBox1SelectedIndexChanged(object sender, EventArgs e)
  {
   this.pictureBox1.ImageLocation=this.listBox1.SelectedItem.ToString();
   this.toolStripStatusLabel1.Text=this.listBox1.SelectedItem.ToString();
  }
  
  void TextBox1KeyUp(object sender, KeyEventArgs e)
  {
   if(e.KeyCode== Keys.Enter)
   {
    this.BtnGoClick(this.btnGo,null);
   }
  }
 }
}


【更多阅读】

  1. [译]用C#检测你的打印机是否连接
  2. [译]C#控制计算机的并口LPT
  3. [原]WMICodeCreator:C#产生WMI代码的工具
  4. [原]SeeFiles:C#查看和修改文件或目录所有属性的工具
  5. [原]使用Excel的VBA来读取和修改bmp位图像素数据
  6. [原]SeeFiles:C#查看和修改文件或目录所有属性的工具
  7. [原]SeeFiles:C#查看和修改文件或目录所有属性的工具
  8. [原]使用Excel的VBA来读取和修改bmp位图像素数据
  9. [译]C#控制计算机的并口LPT
  10. [原]GetAlpha:C#实现获取网页验证码图片,并识别出其中的字母

  • 0
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值