Bonjour,

Dans mon programme j'ai un BackgroundWorker qui lance, par l'intermédiaire de plusieurs Sub, le téléchargement, le redimensionnement et le formatage en .png d'images.
Je peux voir que tout le cheminement suit son cours, jusqu'au Sub SaveImage() où manifestement le handler pour 'bitmap.DownloadCompleted' n'est pas pris en compte!?
Pourriez-vous svp me dire la raison de ceci..et comment -en gardant la structure actuelle de division des taches par plusieurs Subs- je pourrais arriver à ce que les images soient correctement sauvées!?

D’avance un grand merci!!

Code : Sélectionner tout - Visualiser dans une fenêtre à part
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
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
Imports System.ComponentModel
Imports System.IO
Imports System.Net
 
Class MainWindow
    Private Sub Button1_Click(sender As System.Object, e As System.Windows.RoutedEventArgs) Handles Button1.Click
        Dim but As Button = TryCast(sender, Button)
        ProgressBar1.Value = 0
        Dim worker As BackgroundWorker = New BackgroundWorker() With {.WorkerReportsProgress = True, .WorkerSupportsCancellation = True}
        AddHandler worker.DoWork, New DoWorkEventHandler(AddressOf worker_DoWork)
        AddHandler worker.ProgressChanged, New ProgressChangedEventHandler(AddressOf worker_ProgressChanged)
        AddHandler worker.RunWorkerCompleted, New RunWorkerCompletedEventHandler(AddressOf worker_RunWorkerCompleted)
 
        worker.RunWorkerAsync()
        worker.Dispose()
    End Sub
    Sub worker_DoWork(ByVal sender As Object, ByVal e As DoWorkEventArgs)
        Dim dataItemsList As New List(Of String)
        dataItemsList.Add("jntCA3d.jpg")
        dataItemsList.Add("nRVcjd6.jpg")
        dataItemsList.Add("b4PDGb8.jpg")
 
        Dim filePath As String = Environment.GetFolderPath(Environment.SpecialFolder.Desktop)
        filePath = filePath & "\Test\"
        If Not Directory.Exists(filePath) Then
            Directory.CreateDirectory(filePath)
        End If
 
        Dim ProgressPercentage As Integer = 0
        ProgressPercentage = 0
        For Each str As String In dataItemsList
            collectInfoGB(str, filePath)
            ProgressPercentage += 1
            TryCast(sender, BackgroundWorker).ReportProgress(ProgressPercentage, dataItemsList.Count)
        Next
    End Sub
    Private Sub collectInfoGB(ByVal str As String, ByVal path As String)
        Dim fullUrl = "http://i.imgur.com/" & str
        Try
            Dim requ As WebRequest = WebRequest.Create(fullUrl)
            requ.Timeout = 5000
            Dim response As WebResponse = requ.GetResponse()
            Dim stream As Stream = response.GetResponseStream()
 
            If Directory.Exists(path) Then
                Dim bitmap As New BitmapImage()
                bitmap.BeginInit()
                bitmap.UriSource = New Uri(fullUrl)
                bitmap.EndInit()
                '
                SaveImage(bitmap, str, path)
            End If
        Catch webError As WebException
            If webError.Status = WebExceptionStatus.Timeout Then
                Debug.Print("TimeOut exception")
            End If
        End Try
    End Sub
    Sub SaveImage(ByVal bitmap As BitmapImage, ByVal str As String, ByVal path As String)
        Debug.Print("passed")
        If bitmap.IsDownloading Then
            AddHandler bitmap.DownloadCompleted, New EventHandler(Sub(sender As Object, args As EventArgs) processImage(bitmap, str, path))
        Else
            processImage(bitmap, str, path)
        End If
    End Sub
    Sub processImage(ByVal bitmap As BitmapImage, ByVal str As String, ByVal path As String) ' Limit to 640px
        Dim ratio As Decimal = 1
        Dim maxSize As Integer = 640
        Dim width As Integer = bitmap.PixelWidth
        Dim height As Integer = bitmap.PixelHeight
        If Math.Max(width, height) > maxSize Then
            ratio = maxSize / Math.Max(width, height)
        End If
        '
        Dim tbBitmap As New TransformedBitmap(bitmap, New ScaleTransform(ratio, ratio, 0, 0))
        Dim encoder As PngBitmapEncoder = New PngBitmapEncoder()
        encoder.Frames.Add(BitmapFrame.Create(tbBitmap))
        Using filestream = New FileStream(path & "\" & str & ".png", FileMode.Create)
            encoder.Save(filestream)
        End Using
    End Sub
    Private Sub worker_ProgressChanged(ByVal sender As Object, ByVal e As ProgressChangedEventArgs)
        If e.UserState IsNot Nothing Then
            ProgressBar1.Value = CDbl(e.ProgressPercentage / e.UserState * 100)
        End If
    End Sub
    Private Sub worker_RunWorkerCompleted(ByVal sender As Object, ByVal e As RunWorkerCompletedEventArgs)
        '
    End Sub
End Class
Code : Sélectionner tout - Visualiser dans une fenêtre à part
1
2
3
4
5
6
7
8
9
<Window x:Class="MainWindow"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    Title="MainWindow" Height="350" Width="525">
    <Grid>
        <Button Content="Start" Height="23" HorizontalAlignment="Left" Margin="12,12,0,0" Name="Button1" VerticalAlignment="Top" Width="75" />
        <ProgressBar Height="19" HorizontalAlignment="Left" Margin="12,146,0,0" Name="ProgressBar1" VerticalAlignment="Top" Width="479" />
    </Grid>
</Window>