VB picturebox控件问题
3个回答
展开全部
'在窗框内加入command1控件(不要加picture控件),并在窗体代码区复制下面代码,运行,按command1按钮,一幅图片就显示在动态创建的picture控件内,好看!
'====窗体代码====
Option Explicit
Private Sub Command1_Click()
Dim Url As String, SaveFile As String
Dim fso, aso, http, GetRemoteData, i As Integer
On Error Resume Next
Url = "http://t11.baidu.com/it/u=1375248793,2744756601&fm=3&gp=11.jpg" '你可以在此修改成实际下载地址
SaveFile = App.Path & "\1.gif" '你可以在此修改成实际保存地址
Set fso = CreateObject("Scripting.FileSystemObject")
Set aso = CreateObject("ADODB.Stream")
Set http = CreateObject("Microsoft.XMLHTTP")
With http
.Open "Get", Url, False, "", ""
.send
If .ReadyState <> 4 Then Exit Sub
GetRemoteData = .responsebody
End With
If Dir(SaveFile) <> "" Then Kill SaveFile
fso.createtextfile(SaveFile).Close
aso.Type = 1 '数据流类型设为字节'
aso.Open
aso.loadfromfile SaveFile '打开文件'
aso.write GetRemoteData '写入数据'
aso.SaveToFile SaveFile, 2 '保存文件
aso.Close
Set aso = Nothing
Set fso = Nothing
Set http = Nothing
Dim ctlNew
Set ctlNew = Me.Controls.Add("VB.PictureBox", "picNew", Me)
ctlNew.Left = 360
ctlNew.Top = 360
ctlNew.Width = 1800
ctlNew.Height = 1800
ctlNew.Picture = LoadPicture(SaveFile)
ctlNew.Visible = True
End Sub
'====窗体代码====
Option Explicit
Private Sub Command1_Click()
Dim Url As String, SaveFile As String
Dim fso, aso, http, GetRemoteData, i As Integer
On Error Resume Next
Url = "http://t11.baidu.com/it/u=1375248793,2744756601&fm=3&gp=11.jpg" '你可以在此修改成实际下载地址
SaveFile = App.Path & "\1.gif" '你可以在此修改成实际保存地址
Set fso = CreateObject("Scripting.FileSystemObject")
Set aso = CreateObject("ADODB.Stream")
Set http = CreateObject("Microsoft.XMLHTTP")
With http
.Open "Get", Url, False, "", ""
.send
If .ReadyState <> 4 Then Exit Sub
GetRemoteData = .responsebody
End With
If Dir(SaveFile) <> "" Then Kill SaveFile
fso.createtextfile(SaveFile).Close
aso.Type = 1 '数据流类型设为字节'
aso.Open
aso.loadfromfile SaveFile '打开文件'
aso.write GetRemoteData '写入数据'
aso.SaveToFile SaveFile, 2 '保存文件
aso.Close
Set aso = Nothing
Set fso = Nothing
Set http = Nothing
Dim ctlNew
Set ctlNew = Me.Controls.Add("VB.PictureBox", "picNew", Me)
ctlNew.Left = 360
ctlNew.Top = 360
ctlNew.Width = 1800
ctlNew.Height = 1800
ctlNew.Picture = LoadPicture(SaveFile)
ctlNew.Visible = True
End Sub
网易云信
2023-12-06 广告
2023-12-06 广告
UIkit是一套轻量级、模块化且易于使用的开源UI组件库,由YOOtheme团队开发。它提供了丰富的界面元素,包括按钮、表单、表格、对话框、滑块、下拉菜单、选项卡等等,适用于各种类型的网站和应用程序。UIkit还支持响应式设计,可以根据不同...
点击进入详情页
本回答由网易云信提供
展开全部
可以使用USERCONTROL进行异步下载图片
1.在工程中添加一个用户控件,假设名称为UserControl1
添加如下代码
Option Explicit
Dim m_pic As PictureBox
'图片框属性
Public Property Get Pic() As PictureBox
Set Pic = m_pic
End Property
Public Property Set Pic(ByVal var As PictureBox)
Set m_pic = var
End Property
'异步下载图片
Public Sub DownLoad(ByVal strUrl As String)
UserControl.AsyncRead strUrl, vbAsyncTypeByteArray, "", vbAsyncReadForceUpdate
End Sub
'下载完成
Private Sub UserControl_AsyncReadComplete(AsyncProp As AsyncProperty)
Dim F() As Byte
Dim fn As Long
Dim sFile As String
Dim s As String
'写文件,这里文件名可以随机生成
sFile = App.Path & "\02383"
With AsyncProp
If .BytesMax <> 0 Then
If .BytesMax <> .BytesRead Then
'下载失败
Else
'下载成功
fn = FreeFile '获取文件号
F = .Value '文件内容,下载的内容
Open sFile For Binary Access Write As #fn '建立二进制文件
Put #fn, , F '写文件
Close #fn
Erase F '释放数组
Set m_pic.Picture = LoadPicture(sFile) '设置图片属性,显示图片
End If
End If
End With
End Sub
2.在Form1中添加一个刚才的自定义控件,再加一个picturebox,再加一个commandbutton,窗体代码如下
Option Explicit
Private Sub Command1_Click()
Set UserControl11.Pic = Picture1
UserControl11.DownLoad "http://www.baidu.com/img/baidu_logo.gif"
End Sub
1.在工程中添加一个用户控件,假设名称为UserControl1
添加如下代码
Option Explicit
Dim m_pic As PictureBox
'图片框属性
Public Property Get Pic() As PictureBox
Set Pic = m_pic
End Property
Public Property Set Pic(ByVal var As PictureBox)
Set m_pic = var
End Property
'异步下载图片
Public Sub DownLoad(ByVal strUrl As String)
UserControl.AsyncRead strUrl, vbAsyncTypeByteArray, "", vbAsyncReadForceUpdate
End Sub
'下载完成
Private Sub UserControl_AsyncReadComplete(AsyncProp As AsyncProperty)
Dim F() As Byte
Dim fn As Long
Dim sFile As String
Dim s As String
'写文件,这里文件名可以随机生成
sFile = App.Path & "\02383"
With AsyncProp
If .BytesMax <> 0 Then
If .BytesMax <> .BytesRead Then
'下载失败
Else
'下载成功
fn = FreeFile '获取文件号
F = .Value '文件内容,下载的内容
Open sFile For Binary Access Write As #fn '建立二进制文件
Put #fn, , F '写文件
Close #fn
Erase F '释放数组
Set m_pic.Picture = LoadPicture(sFile) '设置图片属性,显示图片
End If
End If
End With
End Sub
2.在Form1中添加一个刚才的自定义控件,再加一个picturebox,再加一个commandbutton,窗体代码如下
Option Explicit
Private Sub Command1_Click()
Set UserControl11.Pic = Picture1
UserControl11.DownLoad "http://www.baidu.com/img/baidu_logo.gif"
End Sub
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
展开全部
把api[URLDownloadToFile]把图片下载到本地 然后用picture控件load这个图片!~
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询