62,046
社区成员
发帖
与我相关
我的任务
分享
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("http://") Then
image__1 = LoadImageFromWeb(imageURI)
Else
image__1 = Image.FromFile(imageURI)
End If
Return image__1
End Function
Public Function LoadImageFromWeb(ByVal sURL As String) As Bitmap
Dim i As Integer = sURL.LastIndexOf("/") + 1
Dim str As String = sURL.Substring(i, sURL.Length - i)
Dim webRequest__1 As Net.WebRequest = Net.WebRequest.Create(sURL)
webRequest__1.Credentials = Net.CredentialCache.DefaultCredentials
Dim stream As Stream = webRequest__1.GetResponse().GetResponseStream()
Dim memoryStream As New MemoryStream()
Dim bs As Byte() = New Byte(255) {}
Dim j As Integer = stream.Read(bs, 0, CInt(bs.Length))
While j > 0
memoryStream.Write(bs, 0, j)
j = stream.Read(bs, 0, CInt(bs.Length))
End While
stream.Close()
memoryStream.Position = CLng(0)
Return New Bitmap(memoryStream)
End Function
'产生文件名
Private Shared constant As Char() = {"0", "1", "2", "3", "4", "5", "6", "7", "8", "9", "a", "b", "c", "d", "e", "f", "g", "h", "i", "j", "k", "l", "m", "n", "o", "p", "q", "r", "s", "t", "u", "v", "w", "x", "y", "z"}
Public Shared Function GenerateRandom(ByVal Length As Integer) As String
Dim newRandom As System.Text.StringBuilder = New System.Text.StringBuilder(62)
Dim rd As Random = New Random
Dim i As Integer = 0
While i < Length
newRandom.Append(constant(rd.Next(36)))
System.Math.Min(System.Threading.Interlocked.Increment(i), i - 1)
End While
Return newRandom.ToString
End Function