Results 1 to 1 of 1

Thread: retrieve image size+download with progressbar ( from the web )

Threaded View

  1. #1

    Thread Starter
    Frenzied Member dynamic_sysop's Avatar
    Join Date
    Jun 2003
    Location
    Ashby, Leicestershire.
    Posts
    1,142

    Post retrieve image size+download with progressbar ( from the web )

    hi dudes, another little project i knocked up for fun . this will allow you to get the size of a file on the net , then download it with a progressbar to indicate how it's going.
    i built one with SavefileDialog option also, but to reduce the code being posted this will save a default image name to your app's bin folder.
    ok , first you need a reference to these 2 at the top of your form ...
    VB Code:
    1. Imports System.Net
    2. Imports System.IO
    then a command button and a progressbar ( Button1 and Progressbar1 ) then drop this code in ...
    VB Code:
    1. Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
    2.         Try
    3.             Dim imgPath As String = "http://www.google.com/images/logo.gif"
    4.  
    5.             Dim wRequest As HttpWebRequest = DirectCast(HttpWebRequest.Create(imgPath), HttpWebRequest)
    6.             Dim wResponse As WebResponse = DirectCast(wRequest.GetResponse(), WebResponse)
    7.  
    8.             Dim sWriter As FileStream = New FileStream("new.gif", FileMode.OpenOrCreate)
    9.             Dim ContentLength As Integer = wResponse.ContentLength
    10.  
    11.             Dim sizeInKB As String = (ContentLength / 1024).ToString()
    12.             Me.Text = "The Size of the Image is: " & sizeInKB.Substring(0, sizeInKB.IndexOf(".") + 3) & "KB"
    13.  
    14.             Dim buffer(ContentLength) As Byte
    15.             Dim length As Integer = ContentLength
    16.             Dim position As Integer = 0
    17.             Dim complete As Integer = 1
    18.             Dim returned As Integer = 0
    19.  
    20.             ProgressBar1.Value = 0 '/// clear the progressbar.
    21.             ProgressBar1.Maximum = ContentLength '/// set it's length.
    22.  
    23.             While Not complete = 0
    24.                 position = wResponse.GetResponseStream().Read(buffer, returned, length)
    25.                 sWriter.Write(buffer, returned, position)
    26.                 complete = position
    27.                 returned += position
    28.                 length -= position
    29.                 ProgressBar1.Step = returned
    30.                 ProgressBar1.PerformStep()
    31.             End While
    32.  
    33.             Me.Text = "Completed download"
    34.             sWriter.Close()
    35.             wRequest = Nothing
    36.  
    37.         Catch ex As Exception
    38.             Console.WriteLine(ex.Message)
    39.         Catch webex As WebException
    40.             Console.WriteLine(webex.Message)
    41.         End Try
    42.     End Sub
    i've included a sample project ...
    Attached Files Attached Files
    ~
    if a post is resolved, please mark it as [Resolved]
    protected string get_Signature(){return Censored;}
    [vbcode][php] please use code tags when posting any code [/php][/vbcode]

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width