1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55
| Imports System.IO
Imports System.Net
Imports System.ComponentModel
Imports System.Windows.Resources
Class MainWindow
Private _destPath$ = "C:\Test"
Private files As New List(Of String)
Private total As Integer
Private Sub MainWindow_Loaded(ByVal sender As Object, ByVal e As System.Windows.RoutedEventArgs) Handles Me.Loaded
If Not Directory.Exists(_destPath) Then Directory.CreateDirectory(_destPath)
For i As Integer = 1 To 100
files.Add("https://lalydo.files.wordpress.com/2010/10/for-the-birds.jpg")
Next
total = files.Count
End Sub
Private Sub Button1_Click(ByVal sender As Object, ByVal e As EventArgs) Handles Button1.Click
If files.Count > 0 Then
DownloadFileEx(files(0))
End If
End Sub
Private Sub DownloadFileEx(ByVal file)
Dim client As New WebClient()
AddHandler client.DownloadProgressChanged, AddressOf DownloadProgressChanged
AddHandler client.OpenReadCompleted, AddressOf DownloadFileCompleted
Label1.Content = "Items remaining: " & files.Count - 1
client.OpenReadAsync(New Uri(file))
End Sub
Private Sub DownloadProgressChanged(ByVal sender As Object, ByVal e As DownloadProgressChangedEventArgs)
ProgressBar1.Value = e.ProgressPercentage
ProgressBar2.Value = total - files.Count + 1
End Sub
Private Sub DownloadFileCompleted(ByVal sender As Object, ByVal e As OpenReadCompletedEventArgs)
Dim sri As Stream = TryCast(e.Result, Stream)
Dim bitmap As New BitmapImage()
bitmap.BeginInit()
bitmap.CacheOption = BitmapCacheOption.OnLoad 'forces the loading of image into memory during EndInit
bitmap.StreamSource = sri
bitmap.EndInit()
sri.Close()
Dim encoder As PngBitmapEncoder = New PngBitmapEncoder()
encoder.Frames.Add(BitmapFrame.Create(bitmap))
Using filestream = New FileStream(_destPath & "\" & "test", FileMode.Create)
encoder.Save(filestream)
End Using
files.RemoveAt(0)
If files.Count > 0 Then
DownloadFileEx(files(0))
Else
Label1.Content = "Download finished"
End If
End Sub
End Class |
Partager