取得页面中所有图片

GetImage.aspx
<% @ Page Language="C#" AutoEventWireup="true" CodeFile="GetImage.aspx.cs" Inherits="GetImage"  %>

<! DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd" >

< html  xmlns ="http://www.w3.org/1999/xhtml"   >
< head  runat ="server" >
    
< title > Get image path </ title >
    
< script  type ="text/javascript" >
        
function ChangeModel(radio)
        
{
            
if(radio.id == "RadioUrl")
            
{
                document.getElementById(
'trUrl').style.display = "block";
                document.getElementById(
'trHtml').style.display = "none";
                document.getElementById(
'tbIsUrl').value = "true";
            }

            
if(radio.id == "RadioHtml")
            
{
                document.getElementById(
'trUrl').style.display = "none";
                document.getElementById(
'trHtml').style.display = "block";
                document.getElementById(
'tbIsUrl').value = "";
            }

        }

        
        
//Maintain the "model" when postback
        window.onload = function()
        
{
            
if(document.getElementById('tbIsUrl').value != "")
            
{
                document.getElementById(
'RadioUrl').checked = true;
                document.getElementById(
'trUrl').style.display = "block";
                document.getElementById(
'trHtml').style.display = "none";
            }

            
if(document.getElementById('tdPathList').innerHTML==" ")
            
{
                 document.getElementById(
'trPathListTitle').style.display = "none";
                document.getElementById(
'trPathListContent').style.display = "none";
            }

        }

        
        
function FillTextFromClipboard(textbox)
        
{
            textbox.value
="";   //clear Old
            if(document.getElementById('CheckboxIsFromClip').checked)
            
{
                textbox.value 
= window.clipboardData.getData('text');
            }

        }

        
        
function GetList()
        
{
            
var saveList=document.getElementsByName('isSave');
            
for(i=0;i<saveList.length;i++)
            
{
                
if(saveList[i].checked == true)
                
{
                    document.getElementById(
'tbHref4Save').value += saveList[i].id;
                    document.getElementById(
'tbHref4Save').value += "+";
                }

            }

            document.getElementById(
'bnExeSave').click();
        }

        
    
</ script >
</ head >
< body >
    
< form  id ="form1"  runat ="server" >
    
< div >
        
< table  width ="80%"  border ="1" >
             
< tr >
                
< td  >
                    Model:
                
</ td >
                 
< td  style ="width:80%" >
                     
< input  id ="RadioUrl"  type ="radio"  name ="radioMode"  onclick ="ChangeModel(this)"  value ="URL"  runat ="server"   />< strong > From URL </ strong >
                     
< input  id ="RadioHtml"  type ="radio"  name ="radioMode"  onclick ="ChangeModel(this)"  checked ="checked"   />< strong > From HTML </ strong >
                     
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
                     
< input  id ="CheckboxIsFromClip"  type ="checkbox"   />< strong > From ClipBoard </ strong >
                     
< asp:CheckBox  ID ="cbIsThumb"  runat ="server"  EnableViewState ="False"  Style ="position: relative"
                         Text
="IsThumb"   /></ td >
            
</ tr >
            
            
< tr  id ="trUrl"  style ="display:none"  runat ="server" >
                
< td >
                    URL:
                
</ td >
                 
< td >
                    
< asp:TextBox  runat ="server"  ID ="tbUrl"  Width ="90%"  onfocus ="FillTextFromClipboard(this)" ></ asp:TextBox >
                
</ td >
            
</ tr >
            
            
< tr  id ="trHtml" >
                
< td  valign ="top"  align ="justify" >
                    Html source:
                
</ td >
                
< td  >
                    
< asp:TextBox  ID ="tbHtmlSource"  runat ="server"  TextMode ="MultiLine"  Width ="100%"  Height ="300px"  onfocus ="FillTextFromClipboard(this)" ></ asp:TextBox >
                
</ td >
            
</ tr >
            
            
< tr >
                
< td >
                    Type:
                
</ td >
                
< td >
                    
< input  id ="RadioJpg"  type ="radio"  name ="imageTypes"  runat ="server"  checked ="true"   />< strong > Jpg </ strong > &nbsp;&nbsp;
                    
< input  id ="RadioBmp"  type ="radio"  name ="imageTypes"  runat ="server"   />< strong > Bmp </ strong > &nbsp;&nbsp;
                    
< input  id ="RadioGif"  type ="radio"  name ="imageTypes"  runat ="server"   />< strong > Gif </ strong > &nbsp;&nbsp;
                
</ td >
            
</ tr >
            
            
< tr >
                
< td  colspan ="2"  align ="center" >
                    
< asp:Button  ID ="bnGetPathList"  runat ="server"  Text ="Get"  OnClick ="bnGetPathList_Click"   /></ td >
            
</ tr >
            
< tr  id ="trPathListTitle" >
                
< td  valign ="top"  align ="justify" >
                    Image path list:
                
</ td >
                 
< td  runat ="server"  id ="tdPathList" >
                 
</ td >
            
</ tr >
            
< tr  id ="trPathListContent" >
                
< td  colspan ="2"  align ="center" >
                    
< input  type ="button"  id ="bnSave"  value ="Save your checked"  onclick ="GetList()"   />
                
</ td >
            
</ tr >
        
</ table >
        
</ div >
        
        
< input  id ="tbIsUrl"  type ="text"  runat ="server"  style ="visibility:hidden"   />
        
< input  id ="tbHref4Save"  type ="text"  runat ="server"  style ="visibility:hidden"   />
        
< asp:Button  ID ="bnExeSave"  runat ="server"  OnClick ="bnExeSave_Click"  Style ="visibility:hidden"  Text ="Save"   />
    
</ form >
</ body >
</ html >

GetImage.aspx.cs:

using  System;
using  System.Data;
using  System.Configuration;
using  System.Collections;
using  System.Web;
using  System.Web.Security;
using  System.Web.UI;
using  System.Web.UI.WebControls;
using  System.Web.UI.WebControls.WebParts;
using  System.Web.UI.HtmlControls;
using  System.Text.RegularExpressions;
using  System.Text;
using  System.IO;
using  System.Net;

public   partial   class  GetImage : System.Web.UI.Page
{
    
protected void Page_Load(object sender, EventArgs e)
    
{
        
if (!IsPostBack)
        
{
        }

    }

    
protected void bnGetPathList_Click(object sender, EventArgs e)
    
{
        
//Get type
        ImageTypes type = new ImageTypes();
        
if (RadioJpg.Checked)
            type 
= ImageTypes.Jpg;
        
if (RadioBmp.Checked)
            type 
= ImageTypes.Bmp;
        
if (RadioGif.Checked)
            type 
= ImageTypes.Gif;

        
if (RadioUrl.Checked)
        
{
            
string url = tbUrl.Text;
            
if (url.Trim() != string.Empty)
            
{
                
// check "http://"
                if (url.Substring(04!= "http")
                
{
                    url 
= "http://" + url;
                }


                
try
                
{
                    
string htmlCode = string.Empty;
                    HttpWebRequest htmlCodeRequest 
= (HttpWebRequest)WebRequest.Create(url);
                    WebResponse htmlCodeResponse 
= htmlCodeRequest.GetResponse();
                    StreamReader htmlReader 
= new StreamReader(htmlCodeResponse.GetResponseStream());
                    htmlCode 
= htmlReader.ReadToEnd();
                    htmlReader.Close();

                    CGetImage.FillHrefToTD(htmlCode, type, tdPathList, cbIsThumb.Checked);
                }

                
catch
                
{
                    
// do nothing
                }

            }

        }

        
else // From html
        {
            
if (tbHtmlSource.Text.Trim() != string.Empty)
            
{
                CGetImage.FillHrefToTD(tbHtmlSource.Text, type, tdPathList, cbIsThumb.Checked);
            }

        }

    }

    
protected void bnExeSave_Click(object sender, EventArgs e)
    
{
        
string[] hrefs = tbHref4Save.Value.Trim().Split('+');

       
// DirectoryInfo dirInfo = new DirectoryInfo(Server.MapPath("images"));
        for (int imageCounter = 0; imageCounter < hrefs.Length; imageCounter++)
        
{
            
if (hrefs[imageCounter].Trim() == string.Empty)
            
{
                
continue;
            }

            
try
            
{
                
string imageName = hrefs[imageCounter].Substring(hrefs[imageCounter].LastIndexOf('/'+ 1);
                WebClient wc 
= new WebClient();
                
string savePath = Server.MapPath("images/" + imageName);
                
//string savePath = newDir + imageName;
                string url = hrefs[imageCounter];
                
if (url.Substring(01== "/")
                    url 
= tbUrl.Text + url;
                
if (url.Substring(04!= "http")
                
{
                    url 
= "http://" + url;
                }

                wc.DownloadFile(url, savePath);
            }

            
catch(Exception ex)
            
{
                Response.Write(ex.Message);
            }

        }

    }

}

App_code/CGetImage.cs

using  System;
using  System.Data;
using  System.Configuration;
using  System.Web;
using  System.Web.Security;
using  System.Web.UI;
using  System.Web.UI.WebControls;
using  System.Web.UI.WebControls.WebParts;
using  System.Web.UI.HtmlControls;
using  System.Text;
using  System.IO;
using  System.Net;
using  System.Text.RegularExpressions;

/// <summary>
/// Summary description for CGetImage
/// </summary>

public   class  CGetImage
{
    
public CGetImage()
    
{
        
//
        
// TODO: Add constructor logic here
        
//
    }


    
/// <summary>
    
/// Fill href to the "result cell"
    
/// </summary>
    
/// <param name="sourceHtml">html code</param>
    
/// <param name="imageType">image type</param>
    
/// <param name="toTD">Cell</param>

    public static void FillHrefToTD(string sourceHtml, ImageTypes imageType,HtmlTableCell toTD,bool isThumb)
    
{
        toTD.InnerHtml 
= string.Empty;
        
string regStr = string.Empty;

        
switch (imageType)
        
{
            
case ImageTypes.Jpg:
                regStr 
= @"http://([w-]+.)+[w-]+(/[w- ./?%&=]*)?.jpg";
                
break;
            
case ImageTypes.Bmp:
                regStr 
= @"http://([w-]+.)+[w-]+(/[w- ./?%&=]*)?.bmp";
                
break;
            
case ImageTypes.Gif:
                regStr 
= @"http://([w-]+.)+[w-]+(/[w- ./?%&=]*)?.gif";
                
break;
        }


        Regex re 
= new Regex(regStr, RegexOptions.IgnoreCase | RegexOptions.Multiline);
        MatchCollection mc 
= re.Matches(sourceHtml);

        HtmlTable tableImageList 
= new HtmlTable();
        
foreach (Match ma in mc)
        
{
            
try
            
{
                
string imageName = ma.Value.Substring(ma.Value.LastIndexOf("/"+ 1);
                
string imageLinker = string.Empty;
                
if (isThumb)
                    imageLinker 
= "<img width='80px' src="" + ma.Value + "">";
                
else
                    imageLinker 
= "<a href="" + ma.Value + "">" + ma.Value + "</a>";
                HtmlTableRow htr 
= new HtmlTableRow();
                HtmlTableCell htcImage 
= new HtmlTableCell();
                htcImage.InnerHtml 
= imageLinker;
                HtmlTableCell htcCheckBox 
= new HtmlTableCell();
                htcCheckBox.InnerHtml 
= "<input type='checkbox' name='isSave' checked='true' id='" + ma.Value + "'></input>";
                htr.Cells.Add(htcImage);
                htr.Cells.Add(htcCheckBox);
                tableImageList.Rows.Add(htr);
            }

            
catch
            
{

            }

        }
// end foreach
        toTD.Controls.Add(tableImageList);
    }

}


/// <summary>
/// Image Types
/// </summary>

public   enum  ImageTypes
{
    Jpg,
    Bmp,
    Gif,
}

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值