softfx
softfx

Reputation: 39

vb.net image download and populate on the picturebox error

i want to download image file from internet and open this image file into picturebox1 on the form.

i changed downloaded picture image name to cap.png

then i open this file onto picturebox1 but have problem in here.

always error like 'file is used by other process ' or 'out of memory'

whole day i try to make it work but i coudn't

if anyone help me much appreciate

            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

Upvotes: 1

Views: 564

Answers (2)

Ivan Stefanov
Ivan Stefanov

Reputation: 393

I don't have enough rep for comments, so I'll wrote here

try to add after client.DownloadFileAsync(myUrl, "cap.png") add one more line client.Dispose()

Upvotes: 1

Ivan Stoev
Ivan Stoev

Reputation: 205829

Use DownloadFile instead of DownloadFileAsync. Or, if you don't need a file, you can use the following:

    static Image DownloadImage(string address)
    {
        using (var client = new WebClient())
            return Image.FromStream(new MemoryStream(client.DownloadData(address)));
    }

Upvotes: 0

Related Questions