Comment puis-je charger un BitmapSource
à partir d'un fichier image?BitmapSource à partir du fichier
18
A
Répondre
6
Vous pouvez lire les octets de l'image du disque dans un tableau d'octets, puis créer votre objet BitmapImage.
var stream = new MemoryStream(imageBytes);
var img = new System.Windows.Media.Imaging.BitmapImage();
img.BeginInit();
img.StreamSource = stream;
img.EndInit();
return img;
2
Le code suivant:
FileStream fileStream =
new FileStream(fileName, FileMode.Open, FileAccess.Read);
var img = new System.Windows.Media.Imaging.BitmapImage();
img.BeginInit();
img.StreamSource = fileStream;
img.EndInit();
31
Cela fonctionne pour moi:
BitmapSource bSource = new BitmapImage(new Uri("c:\\image.bmp"));
BitmapImage bImage = new BitmapImage(new Uri("c:\\image.bmp"));
Questions connexes
- 1. Générer BitmapSource à partir de UIElement
- 2. WPF - Générer BitmapSource à partir de l'état Over d'un bouton
- 3. Lecture à partir du fichier
- 4. Fuite de mémoire lors du chargement asynchrone des images BitmapSource
- 5. lire et écrire à partir du fichier
- 6. php lu à partir du fichier float
- 7. iphone lu à partir du fichier
- 8. Rails import à partir du fichier Excel
- 9. Get module.exports à partir du même fichier
- 10. SoundPool.load() et FileDescriptor à partir du fichier
- 11. DataGridView à partir du fichier binaire
- 12. réponse rediriger à partir du fichier .ashx
- 13. Java Entrée à partir du fichier formaté
- 14. générer tableview à partir du fichier plist
- 15. MP3 à partir du fichier distant
- 16. Lecture à partir du fichier en eclipse
- 17. valeur unique à partir du fichier xml
- 18. onload à partir du fichier js externe
- 19. Remplissage DataTable à partir du fichier
- 20. Obtention du fichier .java à partir du nom du package
- 21. emplacement du fichier à System.IO.Stream
- 22. WPF/BackgroundWorker et le problème BitmapSource
- 23. Comment capturer un Bitmap créé à partir d'un contrôle Windows Forms vers un BitmapSource?
- 24. Détermination du nom de fichier (ouvert) à partir d'un fichier *
- 25. Comment extraire le nom du fichier à partir du nom du chemin du fichier?
- 26. Appelez une méthode à partir d'un fichier inclus PAS à partir du fichier inclu. Php
- 27. Lecture du corps HTML à partir du fichier MSG Outlook
- 28. Retour du fichier à partir du module python
- 29. Retour du fichier Docx à partir du service Web Asp.net
- 30. Définition du curseur personnalisé à partir du fichier de ressources
Mais cette fuites MemoryStream! Vous devez définir CacheOption = OnLoad et disposer le flux par la suite. – Vlad