C# vb.net图像下载并在picturebox上填充错误

C# vb.net图像下载并在picturebox上填充错误,c#,vb.net,C#,Vb.net,我想从internet下载图像文件,并将此图像文件打开到表单上的picturebox1中 我将下载的图片名称改为cap.png 然后我在picturebox1上打开这个文件,但这里有问题 总是出现类似“文件被其他进程使用”或“内存不足”的错误 我一整天都在努力,但我做不到 如果有人帮我,我会非常感激的 Dim client As New WebClient remBadstring = captchaMatch.Value.Replace(""""

我想从internet下载图像文件,并将此图像文件打开到表单上的picturebox1中

我将下载的图片名称改为cap.png

然后我在picturebox1上打开这个文件,但这里有问题

总是出现类似“文件被其他进程使用”或“内存不足”的错误

我一整天都在努力,但我做不到

如果有人帮我,我会非常感激的

            Dim client As New WebClient
            remBadstring = captchaMatch.Value.Replace("""", "")
            Dim myUrl = New Uri(remBadstring)
            client.DownloadFileAsync(myUrl, "cap.png")
            'My.Computer.Network.DownloadFile(myUrl, Application.StartupPath & "/cap.png")              
            Dim img As Image
            Dim tmp As Image = Image.FromFile("cap.png")
            img = New Bitmap(tmp)
            tmp.Dispose()
            PictureBox1.Load(remBadstring) ' error hapen. 
            'PictureBox1.Image = Image.FromFile("cap.png")
            Application.DoEvents()
            Dim myUrl2 As String = remBadstring

使用DownloadFile而不是DownloadFileAsync。或者,如果您不需要文件,可以使用以下选项:

    static Image DownloadImage(string address)
    {
        using (var client = new WebClient())
            return Image.FromStream(new MemoryStream(client.DownloadData(address)));
    }
我没有足够的评论代表,所以我会在这里写


尝试在
client.DownloadFileAsync(myUrl,“cap.png”)
之后添加一行
client.Dispose()

非常抱歉,我更改了标签