Bonjour,
Le code suivant charge unflowLayoutPanel1.Controls avec des images d'un répertoire, puis les décharge avant de les supprimer du répertoire.
J'ai supprimé tout le code inutile.
Le problème est une erreur lors de la tentative de suppression du fichier qui est encore utilisé par un autre processus alors qu'il ne l'était pas avant l'appel de la fonction.
Je ne trouve pas le problème car normalement l'instruction pictureBox.Image.Dispose(); devrait libérer le fichier.
Est-ce que quelqu'un peut m'aider car je suis bloqué sur ce problème depuis plusieurs jours ?
P-S : même problème avec pictureBox.Image = (System.Drawing.Image)System.Drawing.Image.FromFile(photoFile).Clone();
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 private void LoadPhotos(string directory) { string[] photoFiles = System.IO.Directory.GetFiles(directory, "*.jpg"); // Filtre pour les fichiers images (vous pouvez modifier selon vos besoins) try { flowLayoutPanel1.Controls.Clear(); foreach (string photoFile in photoFiles) { PictureBox pictureBox = new PictureBox(); pictureBox.Image = (System.Drawing.Image)System.Drawing.Image.FromFile(photoFile); pictureBox.ImageLocation = photoFile; pictureBox.SizeMode = PictureBoxSizeMode.StretchImage; pictureBox.Width = 150; pictureBox.Height = 150; pictureBox.Margin = new Padding(5); flowLayoutPanel1.Controls.Add(pictureBox); System.Windows.Forms.Application.DoEvents(); } } catch (Exception ex) { MessageBox.Show("Une erreur s'est produite lors du chargement des photos : " + ex.Message); } // Libération des fichiers avant leur effacement foreach (PictureBox pictureBox in flowLayoutPanel1.Controls) { string imageLocation = pictureBox.ImageLocation; pictureBox.Image.Dispose(); pictureBox.Dispose(); File.Delete(imageLocation); } }
Partager