VB.net 2010 视频教程 VB.net 2010 视频教程 python基础视频教程
SQL Server 2008 视频教程 c#入门经典教程 Visual Basic从门到精通视频教程
当前位置:
首页 > VB.net教程 >
  • VB.NET 画面PictureBox中显示网络图片的多种方式。

1.获取网络动态图片的api,设置图片显示。
   Dim wr As WebRequest = WebRequest.Create("https://uploadbeta.com/api/pictures/random/?key=BingEverydayWallpaperPicture")
   Dim res As WebResponse = wr.GetResponse
   Dim bmp As Bitmap = New Bitmap(res.GetResponseStream)
   Me.PictureBox1.Image = bmp
2.抓取网页中的图片,下载到本地文件夹,然后再显示。
(因为使用了1的方法,这里是完成部分的代码,里面的方法可以参考使用)
Pic_net.vb
 
Imports System.IO
Imports System.Net
Imports System.Text.RegularExpressions
 
Public Class Pic_net
    Private Obj As New ArrayList
 
    Public jpg_obj As New ArrayList
 
    Public Function NetGetData(ByVal Url As String, Optional ByVal GetData As String = "") As String
 
        Dim tmp As String = ""
 
        Try
            '根据网站链接,获取网页数据。
            Dim Request As HttpWebRequest
            Request = CType(WebRequest.Create(Url + "?" + GetData), HttpWebRequest)
            Request.Method = "GET"
 
            Dim Strm As Stream
            Strm = Request.GetResponse().GetResponseStream
 
            Dim Sr As StreamReader = New StreamReader(Strm)
            tmp = Sr.ReadToEnd
 
        Catch ex As Exception
 
            MsgBox(ex.Message)
 
        End Try
 
        Return tmp
 
    End Function
 
 
    Public Function Get_list(ByVal Arr As Object, ByRef tf As Boolean, ByRef XREG As String) As Object
 
        Dim tmp As Object()
 
        Try
            '解析出<img>标签。
            Dim reg 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]*>")
            Dim match As MatchCollection = reg.Matches(CType(Arr, String))
            ReDim tmp(match.Count)
 
            If match.Count = 0 Then
                tf = False
            Else
                tf = True
            End If
 
            Dim i As Integer = 0
            For Each m As Match In match
 
                tmp(i) = m.Groups("imgUrl").Value
                i += 1
 
                Application.DoEvents()
            Next
 
        Catch ex As Exception
            MsgBox(ex.Message)
        End Try
        Return tmp
 
    End Function
 
    Public Function GET_ZJADD(ByVal xstart As Integer, ByVal xend As Integer) As Boolean
       
        Dim tf As Boolean
        Dim tmp As Object()
 
        For i As Integer = xstart To xend
            '通过NetGetData方法获取img中的图片url。
            tmp = CType(Get_list(NetGetData("网址" & i & "/"), tf, ""), Object())
 
            If tf = True Then
                For Each xl As String In tmp
                    If xl <> Nothing Then
                        Obj.Add(xl)
                    End If
                Next
                jpg_obj = Obj
            End If
 
            Application.DoEvents()
        Next
 
        Return True
 
    End Function
 
    Public Function FileDown(ByVal URL As String, ByVal saveFile As String) As Boolean
 
        Dim myWebclient As WebClient
        Try
            '下载图片
            myWebclient = New WebClient()
            myWebclient.DownloadFileAsync(New Uri(URL), saveFile)
            Return True
 
        Catch ex As Exception
            Return False
        End Try
    End Function
End Class
显示图片的代码
 
Dim strImage As String = Down_pic()
Try
  Dim wr As WebRequest = WebRequest.Create(strImage)
  Dim res As WebResponse = wr.GetResponse
  Dim bmp As New Bitmap(res.GetResponseStream)
  '显示图片
  Me.PictureBox1.Image = bmp
Catch ex As Exception
  Me.PictureBox1.Image = Nothing
End Try
 
Function Down_pic() As String
 
        Dim pic As New Pic_net
        Dim strImage As String
        Get_path()
 
        pic.GET_ZJADD(1, 1)
 
        Dim arr As ArrayList = pic.jpg_obj
        Dim i As Integer = arr.Count
        Dim J As Integer = 1
 
        For Each xl As String In arr
 
            Dim str As String = xl
 
            If InStr(str.ToLower, ".gif") > 0 Then
                '下载图片
                pic.FileDown(xl, "C:\Users\" & Environment.UserName & "\Desktop\PIC\" & J & ".gif")
 
            ElseIf InStr(str.ToLower, ".jpg") > 0 Then
 
                pic.FileDown(xl, "C:\Users\" & Environment.UserName & "\Desktop\PIC\" & J & ".jpg")
 
            End If
            J += 1
 
            Application.DoEvents()
 
        Next
        
        '这里可以从下载的图片选一个显示。
        Return strImage
 
 
 
    End Function
 
 
    Function Get_path() As Boolean
        '本地路径
        Dim path As String = "C:\Users\" & Environment.UserName & "\Desktop\PIC\"
 
        If Directory.Exists(path) = False Then
 
            Directory.CreateDirectory(path)
 
            Return True
 
        Else
 
            Return False
 
        End If
 
    End Function
 
 
 
 
如果资源学习了,可以点赞支持!
————————————————
版权声明:本文为CSDN博主「聖少俊」的原创文章,遵循CC 4.0 BY-SA版权协议,转载请附上原文出处链接及本声明。
原文链接:https://blog.csdn.net/ningyingjun/article/details/108833336

 

相关教程