vb.net downloaddataAsync to memorystream不起作用



我有以下代码,可以将图片直接加载到我的图片框中(来自内存):

PictureBox1.Image = New Bitmap(New IO.MemoryStream(New Net.WebClient().DownloadData("LINK")))

这里的问题是我的应用程序在下载网络时冻结,所以我想我会使用 DownloadDataAsync

但是,使用此代码根本不起作用:

PictureBox1.Image = New Bitmap(New IO.MemoryStream(New Net.WebClient().DownloadDataAsync(New Uri("LINK"))))

它返回错误"表达式不产生值"

作为错误消息指出,您不能简单地将 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

相关内容

  • 没有找到相关文章

最新更新