[Okta Webinar] Learn how to a build a cloud-first strategyRegister Now


Download Image from Web to File Using VB.Net

Posted on 2009-12-16
Medium Priority
Last Modified: 2013-11-26
Hi to all,

I want to be able to download an image from the web and save it locally to a file in a winforms application. How would I do that using VB.Net?

Question by:ALawrence007

Expert Comment

ID: 26063343
What information do you have?
LVL 70

Assisted Solution

by:Éric Moreau
Éric Moreau earned 600 total points
ID: 26063403
LVL 86

Accepted Solution

Mike Tomlinson earned 1400 total points
ID: 26063552
Haven't looked...but this is probably similar to emoreau's approach:
Imports System.IO
Imports System.Net
Public Class Form1
    Private WithEvents bgw As New System.ComponentModel.BackgroundWorker
    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
        Dim puppyURL As String = "http://www.shooterdog.com/puppy.jpg"
    End Sub
    Private Sub bgw_DoWork(ByVal sender As Object, ByVal e As System.ComponentModel.DoWorkEventArgs) Handles bgw.DoWork
        Dim url As String = e.Argument
        Dim img As Image = GetWebImage(url)
        e.Result = img
    End Sub
    Private Function GetWebImage(ByVal URL As String) As Image
            Dim wReq As WebRequest = WebRequest.Create(URL)
            If Not IsNothing(wReq) Then
                Dim wRes As WebResponse = wReq.GetResponse
                If Not IsNothing(wRes) Then
                    Dim wStrm As Stream = wRes.GetResponseStream
                    If Not IsNothing(wStrm) Then
                        Return Image.FromStream(wStrm)
                    End If
                End If
            End If
        Catch ex As Exception
        End Try
        Return Nothing
    End Function
    Private Sub bgw_RunWorkerCompleted(ByVal sender As Object, ByVal e As System.ComponentModel.RunWorkerCompletedEventArgs) Handles bgw.RunWorkerCompleted
        Dim img As Image = e.Result
        If Not IsNothing(img) Then
            PictureBox1.Image = img
            ' if you want to save it:
            img.Save(System.IO.Path.Combine(My.Computer.FileSystem.SpecialDirectories.MyDocuments, "SomeFile.jpg"), System.Drawing.Imaging.ImageFormat.Jpeg)
            MessageBox.Show("Failed to get image...")
        End If
    End Sub
End Class

Open in new window


Author Closing Comment

ID: 31666778
Thanks Guys!

Featured Post

Vote for the Most Valuable Expert

It’s time to recognize experts that go above and beyond with helpful solutions and engagement on site. Choose from the top experts in the Hall of Fame or on the right rail of your favorite topic page. Look for the blue “Nominate” button on their profile to vote.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Creating an analog clock UserControl seems fairly straight forward.  It is, after all, essentially just a circle with several lines in it!  Two common approaches for rendering an analog clock typically involve either manually calculating points with…
A long time ago (May 2011), I have written an article showing you how to create a DLL using Visual Studio 2005 to be hosted in SQL Server 2005. That was valid at that time and it is still valid if you are still using these versions. You can still re…
Please read the paragraph below before following the instructions in the video — there are important caveats in the paragraph that I did not mention in the video. If your PaperPort 12 or PaperPort 14 is failing to start, or crashing, or hanging, …
Is your data getting by on basic protection measures? In today’s climate of debilitating malware and ransomware—like WannaCry—that may not be enough. You need to establish more than basics, like a recovery plan that protects both data and endpoints.…
Suggested Courses

834 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question