VB.net DownloadDataAsync 到 MemoryStream 不工作
VB.net DownloadDataAsync to MemoryStream not working
我有以下代码可以将互联网上的图片直接加载到我的图片框中(从内存中):
PictureBox1.Image = New Bitmap(New IO.MemoryStream(New Net.WebClient().DownloadData("LINK")))
这里的问题是我的应用程序在 WebClient 下载时冻结,所以我想我会使用 DownloadDataAsync
但是,使用此代码根本不起作用:
PictureBox1.Image = New Bitmap(New IO.MemoryStream(New Net.WebClient().DownloadDataAsync(New Uri("LINK"))))
它returns错误"Expression does not produce a value"
如错误消息所述,您不能简单地将 DownloadDataAsync
作为 MemoryStream
参数传递,因为 DownloadDataAsync
是一个 Sub 而 DownloadData
是一个返回 Bytes()
.
为了使用 DownloadDataSync
,查看下面的示例代码:
Dim wc As New Net.WebClient()
AddHandler wc.DownloadDataCompleted, AddressOf DownloadDataCompleted
AddHandler wc.DownloadProgressChanged, AddressOf DownloadProgressChanged ' in case you want to monitor download progress
wc.DownloadDataAsync(New uri("link"))
以下是事件处理程序:
Sub DownloadDataCompleted(sender As Object, e As DownloadDataCompletedEventArgs)
' If the request was not canceled and did not throw
' an exception, display the resource.
If e.Cancelled = False AndAlso e.Error Is Nothing Then
PictureBox1.Image = New Bitmap(New IO.MemoryStream(e.Result))
End If
End Sub
Sub DownloadProgressChanged(sender As Object, e As DownloadProgressChangedEventArgs)
' show progress using :
' Percent = e.ProgressPercentage
' Text = $"{e.BytesReceived} of {e.TotalBytesToReceive}"
End Sub
我有以下代码可以将互联网上的图片直接加载到我的图片框中(从内存中):
PictureBox1.Image = New Bitmap(New IO.MemoryStream(New Net.WebClient().DownloadData("LINK")))
这里的问题是我的应用程序在 WebClient 下载时冻结,所以我想我会使用 DownloadDataAsync
但是,使用此代码根本不起作用:
PictureBox1.Image = New Bitmap(New IO.MemoryStream(New Net.WebClient().DownloadDataAsync(New Uri("LINK"))))
它returns错误"Expression does not produce a value"
如错误消息所述,您不能简单地将 DownloadDataAsync
作为 MemoryStream
参数传递,因为 DownloadDataAsync
是一个 Sub 而 DownloadData
是一个返回 Bytes()
.
为了使用 DownloadDataSync
,查看下面的示例代码:
Dim wc As New Net.WebClient()
AddHandler wc.DownloadDataCompleted, AddressOf DownloadDataCompleted
AddHandler wc.DownloadProgressChanged, AddressOf DownloadProgressChanged ' in case you want to monitor download progress
wc.DownloadDataAsync(New uri("link"))
以下是事件处理程序:
Sub DownloadDataCompleted(sender As Object, e As DownloadDataCompletedEventArgs)
' If the request was not canceled and did not throw
' an exception, display the resource.
If e.Cancelled = False AndAlso e.Error Is Nothing Then
PictureBox1.Image = New Bitmap(New IO.MemoryStream(e.Result))
End If
End Sub
Sub DownloadProgressChanged(sender As Object, e As DownloadProgressChangedEventArgs)
' show progress using :
' Percent = e.ProgressPercentage
' Text = $"{e.BytesReceived} of {e.TotalBytesToReceive}"
End Sub