I have this code :
for downloading image from the web

PictureBox1.ImageLocation =""

and i want to add
Progress Bar

if start download the image start the Progress Bar
and when finished the download Progress Bar =100

You'll need to know the size of the picture you're downloading and the current downloaded size.

Set the maximum progressbar to the size of the picture and the minimum to 0.

Create a function that will update the progressbarr for every downloaded byte.
(I don't remeber how to do this, so I'll leave to you).

Hope it helps.

If you do not know the image size

If you do not know the image size

This almost never occurs. File sizes are one of the first data that is transmitted.

Btw, by size I mean FILE size, not file dimensions.

Like, the image has 500Kb, not the image is 720x480p.

See if this helps.
2 Buttons (Button1, Button2), 1 ProgressBar

Public Class Form1
    '// Url for Paint.NET Download. ( http://www.getpaint.net/ )
    Private myURL As String = "http://www.dotpdn.com/files/Paint.NET.3.5.6.Install.zip"
    '// File Location Saved to Desktop with the Original File Name and Extension.
    Private myFile As String = Environment.GetFolderPath(Environment.SpecialFolder.Desktop) & myURL.Substring(myURL.LastIndexOf("/"))
    '// WebClient for Downloading File.
    Private WithEvents myWebClient As New Net.WebClient()

    Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        Button1.Text = "Download" : Button2.Text = "Cancel"
    End Sub

    '// Download.
    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
        myWebClient = New Net.WebClient
        ProgressBar1.Value = 0
        Me.Text = "Downloading..."
        myWebClient.DownloadFileAsync(New Uri(myURL), myFile)
        Button1.Enabled = False
    End Sub
    '// Cancel Download.
    Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click
        myWebClient.CancelAsync()
        Application.DoEvents()
        Button1.Enabled = True
        Me.Text = "Download Canceled"
    End Sub

    Private Sub myWebClient_DownloadFileCompleted(ByVal sender As Object, ByVal e As System.ComponentModel.AsyncCompletedEventArgs) Handles myWebClient.DownloadFileCompleted
        If ProgressBar1.Value = ProgressBar1.Maximum Then
            MsgBox("Download Completed", MsgBoxStyle.Information)
            Button1.Enabled = True
        Else
            IO.File.Delete(myFile) '// Delete File if Download is NOT Complete.
        End If
    End Sub
    '// Download Progress.
    Public Sub myWebClient_DownloadProgressChanged(ByVal sender As Object, ByVal e As System.Net.DownloadProgressChangedEventArgs) Handles myWebClient.DownloadProgressChanged
        Try
            Dim dDownloadProgress As Decimal = CStr(e.BytesReceived / e.TotalBytesToReceive * 100)
            ProgressBar1.Value = CInt(dDownloadProgress)
            '// Display Download Status.
            Dim dDownloadedMB As Decimal = Format((e.BytesReceived / 1024) / 1024, "###,###,##0.00")
            Dim dTotalToDownloadMB As Decimal = Format((e.TotalBytesToReceive / 1024) / 1024, "###,###,##0.00")
            Me.Text = "Downloaded: " & dDownloadedMB & "MB out of " & dTotalToDownloadMB & "MB - Progress: " _
                & Format(dDownloadProgress, "###.00") & "%"
        Catch ex As Exception
        End Try
    End Sub
End Class
commented: Very Nice +1
commented: Good job bro! Well done! +1

Man, that download code is PERFECT.

Could you just explain it a little more, so I (and everyone) can easily use it later?

Thank you so much!

Will I get cookies and milk if I do so?:D
-------
To start of, you will need to specify your Web File's Location that you want to download, here:

Private myURL As String = "http://www.dotpdn.com/files/Paint.NET.3.5.6.Install.zip"

..For example, to download the DaniWeb.com logo, use:

Private myURL As String = "http://www.daniweb.com/rxrimages/logo.gif"

Next, you will need to specify a location for the file on your computer.
This line saves the File to a user's Desktop with the original File Name by getting all the text following the ".LastIndexOf("/")", which usually is only the FileName, although in some cases it will not be.

Private myFile As String = Environment.GetFolderPath(Environment.SpecialFolder.Desktop) & myURL.Substring(myURL.LastIndexOf("/"))

..You can specify a Location on your computer and give it a file name of it's own. A simple SaveFileDialog could be of use for such.

Private myFile As String = "C:\myCoolDaniWebLogo.gif"

The rest of the code should be self explanatory.

I added "WithEvents" to the declaration for the WebClient, to display the WebClient's Events.
Can be located in the right ComboBox in the top of the code window once the WebClient is selected in the left ComboBox.

Private WithEvents myWebClient As New Net.WebClient()

Button1 declares the WebClient as a New WebClient.
Sets the ProgressBar Value to 0.
Me.Text...
Starts the Download. myWebClient.DownloadFileAsync("Web File Location", "Your Location for the File on your computer") Button1...

Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
        myWebClient = New Net.WebClient
        ProgressBar1.Value = 0
        Me.Text = "Downloading..."
        myWebClient.DownloadFileAsync(New Uri(myURL), myFile)
        Button1.Enabled = False
    End Sub

Button2 Cancels the Download.
Waits for the Cancel by letting the Application.DoEvents() and ...

Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click
        myWebClient.CancelAsync()
        Application.DoEvents()
        Button1.Enabled = True
        Me.Text = "Download Canceled"
    End Sub

Since for some odd reason it would display the MsgBox("Download Completed", MsgBoxStyle.Information) when you would cancel the download, I added code to check for ProgressBar1.Value if it is at it's Maximum.
..If it is not, then it will delete the file that it was trying to download since restarting the download would start from the beginning and not where it stopped downloading at. Pointless to have a File on the user's end, if not a valid one.

Private Sub myWebClient_DownloadFileCompleted(ByVal sender As Object, ByVal e As System.ComponentModel.AsyncCompletedEventArgs) Handles myWebClient.DownloadFileCompleted
        If ProgressBar1.Value = ProgressBar1.Maximum Then
            MsgBox("Download Completed", MsgBoxStyle.Information)
            Button1.Enabled = True
        Else
            IO.File.Delete(myFile) '// Delete File if Download is NOT Complete.
        End If
    End Sub

This part shows the progress of the download in the ProgressBar and I also added some extra info that is displayed in the Form's Title, as to the Size of the File to download, the current status on the size of the File downloading, andAlso the Percentage of the ProgressBar's Value.

Public Sub myWebClient_DownloadProgressChanged(ByVal sender As Object, ByVal e As System.Net.DownloadProgressChangedEventArgs) Handles myWebClient.DownloadProgressChanged
        Try
            Dim dDownloadProgress As Decimal = CDec(CStr(e.BytesReceived / e.TotalBytesToReceive * 100))
            ProgressBar1.Value = CInt(dDownloadProgress)
            '// Display Download Status.
            Dim dDownloadedMB As Decimal = CDec(Format((e.BytesReceived / 1024) / 1024, "###,###,##0.00"))
            Dim dTotalToDownloadMB As Decimal = CDec(Format((e.TotalBytesToReceive / 1024) / 1024, "###,###,##0.00"))
            Me.Text = "Downloaded: " & dDownloadedMB & "MB out of " & dTotalToDownloadMB & "MB - Progress: " _
                & Format(dDownloadProgress, "###.00") & "%"
        Catch ex As Exception
        End Try
    End Sub

In this last part of the code, I could have declared all the Declarations outside the Public Sub myWebClient_DownloadProgressChanged Sub, just to not constantly declare them every time the Progress Changes, but as mentioned in the previous post by "RenanLazarotto", to make it less confusing for "others".

----------
If this helps, just milk will do, I still have cookies.:D

Man, that code is slick! Codeorder, you are a major help to a lot of people! Folks need to give this guy some rep points, well deserved!

thanks look at that

Dim down As New System.Net.WebClient
        Dim SavePath As String = "C:\x.jpg"
        Dim source As String = "http://www.daniweb.com/rxrimages/logo.gif"
        down.DownloadFile(source, SavePath)
        PictureBox1.Load(SavePath)

i have 6 items of resolution
320
600
720
1024
1080
1890

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.