您的位置:首页 > 其它

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

2012-09-24 16:40 281 查看
用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);
}
}
}
}

【更多阅读】

[译]用C#检测你的打印机是否连接
[译]C#控制计算机的并口LPT
[原]WMICodeCreator:C#产生WMI代码的工具
[原]SeeFiles:C#查看和修改文件或目录所有属性的工具
[原]使用Excel的VBA来读取和修改bmp位图像素数据
[原]SeeFiles:C#查看和修改文件或目录所有属性的工具
[原]SeeFiles:C#查看和修改文件或目录所有属性的工具
[原]使用Excel的VBA来读取和修改bmp位图像素数据
[译]C#控制计算机的并口LPT
[原]GetAlpha:C#实现获取网页验证码图片,并识别出其中的字母
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: 
相关文章推荐