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

 

 

如果资源学习了,可以点赞支持!

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值