-
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
栏目列表
最新更新
python爬虫及其可视化
使用python爬取豆瓣电影短评评论内容
nodejs爬虫
Python正则表达式完全指南
爬取豆瓣Top250图书数据
shp 地图文件批量添加字段
爬虫小试牛刀(爬取学校通知公告)
【python基础】函数-初识函数
【python基础】函数-返回值
HTTP请求:requests模块基础使用必知必会
SQL SERVER中递归
2个场景实例讲解GaussDB(DWS)基表统计信息估
常用的 SQL Server 关键字及其含义
动手分析SQL Server中的事务中使用的锁
openGauss内核分析:SQL by pass & 经典执行
一招教你如何高效批量导入与更新数据
天天写SQL,这些神奇的特性你知道吗?
openGauss内核分析:执行计划生成
[IM002]Navicat ODBC驱动器管理器 未发现数据
初入Sql Server 之 存储过程的简单使用
uniapp/H5 获取手机桌面壁纸 (静态壁纸)
[前端] DNS解析与优化
为什么在js中需要添加addEventListener()?
JS模块化系统
js通过Object.defineProperty() 定义和控制对象
这是目前我见过最好的跨域解决方案!
减少回流与重绘
减少回流与重绘
如何使用KrpanoToolJS在浏览器切图
performance.now() 与 Date.now() 对比