Download Image from Web to File Using VB.Net

Posted on 2009-12-16
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
    LVL 7

    Expert Comment

    What information do you have?
    LVL 69

    Assisted Solution

    by:Éric Moreau
    LVL 85

    Accepted Solution

    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 = ""
        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

    Thanks Guys!

    Featured Post

    IT, Stop Being Called Into Every Meeting

    Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

    Join & Write a Comment

    Suggested Solutions

    Many of us here at EE write code. Many of us write exceptional code; just as many of us write exception-prone code. As we all should know, exceptions are a mechanism for handling errors which are typically out of our control. From database errors, t…
    The ECB site provides FX rates for major currencies since its inception in 1999 in the form of an XML feed. The files have the following format (reducted for brevity) (CODE) There are three files available HERE (…
    To add imagery to an HTML email signature, you have two options available to you. You can either add a logo/image by embedding it directly into the signature or hosting it externally and linking to it. The vast majority of email clients display l…
    Illustrator's Shape Builder tool will let you combine shapes visually and interactively. This video shows the Mac version, but the tool works the same way in Windows. To follow along with this video, you can draw your own shapes or download the file…

    729 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

    Need Help in Real-Time?

    Connect with top rated Experts

    15 Experts available now in Live!

    Get 1:1 Help Now