日期:2014-05-17 浏览次数:20578 次
Protected Sub Button4_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles Button4.Click '建立文件夹 Dim s_rpath As String = System.Configuration.ConfigurationSettings.AppSettings("fileUrl") Dim Datedir As String = DateTime.Now.ToString("yyyyMMdd") & "/x_more" Dim updir As String = s_rpath & "\" & Datedir If Not Directory.Exists(updir) Then Directory.CreateDirectory(updir) End If '获取字符串 Dim sHtmlText As String = a_img_more.Value Dim regImg As New Regex("<img\b[^<>]*?\bsrc[\s\t\r\n]*=[\s\t\r\n]*[""']?[\s\t\r\n]*(?<imgUrl>[^\s\t\r\n""'<>]*)[^<>]*?/?[\s\t\r\n]*>", RegexOptions.IgnoreCase) Dim matches As MatchCollection = regImg.Matches(sHtmlText) Dim fileName As String Dim pid As Integer = Request("id") Dim jsq As Integer = 0 '定义计数器 For Each match As Match In matches Dim imgurl As String = match.Groups("imgUrl").Value If (imgurl.IndexOf("img.xxx.com") < 0) Then '获取不为本站链接的图片 '检测图片尺寸(暂定高或宽大于300px的便下载) Dim image As System.Drawing.Image = LoadImage(imgurl) If image.Width >= 300 Or image.Height >= 300 Then fileName = pid & "_" & GenerateRandom(8) & ".jpg" Dim wc As Net.WebClient = New System.Net.WebClient() wc.DownloadFile(imgurl, updir & "/" & fileName) sHtmlText = sHtmlText.Replace(imgurl, "http://img.xxx.com/" & Datedir & "/" & fileName) jsq = jsq + 1 End If End If Next a_img_more.Value = sHtmlText ClientScript.RegisterStartupScript(Page.[GetType](), "", "<script>alert('共计下载" & jsq & "张图片,更改尚未保存到数据库中,如无误请点击[直接保存内容]!');</script>") End Sub '下面两个类的作用是获取网络图片的属性,目前使用的是高和宽 Public Function LoadImage(ByVal imageURI As String) As Image Dim image__1 As Image If imageURI.StartsWith(&quo