栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > .Net

VB.NET 画面PictureBox中显示网络图片的多种方式。

.Net 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

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
            '解析出标签。
            Dim reg As New Regex("]*?bsrc[strn]*=[strn]*[""']?[strn]*(?[^strn""'<>]*)[^<>]*?/?[strn]*>")
            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 & "DesktopPIC" & J & ".gif")

            ElseIf InStr(str.ToLower, ".jpg") > 0 Then

                pic.FileDown(xl, "C:Users" & Environment.UserName & "DesktopPIC" & 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 & "DesktopPIC"

        If Directory.Exists(path) = False Then

            Directory.CreateDirectory(path)

            Return True

        Else

            Return False

        End If

    End Function

 

 

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

 

转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/908510.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号