Sprache: C#
Wenn man in WPF ein BitmapImage von einer Bilddatei erstellt, so wird ein Stream auf die Datei offen gelassen, sodass das Bild wirklich erst geladen wird, wenn es benötigt wird. Dieses Snippet lädt die Bilddatei sofort in den Arbeitsspeicher und gibt die Datei anschließend wieder frei.
[b]Benötigte Namespaces[/b]
System
System.Windows.Media.Imaging
public static BitmapImage GetBitmapImage(string imageFilePath)
{
BitmapImage bi = new BitmapImage();
bi.BeginInit();
bi.CacheOption = BitmapCacheOption.OnLoad;
bi.UriSource = new Uri(imageFilePath, UriKind.RelativeOrAbsolute);
bi.EndInit();
return bi;
}
public static BitmapImage GetBitmapImage(string imageFilePath)
{
BitmapImage bi = new BitmapImage();
bi.BeginInit();
bi.CacheOption = BitmapCacheOption.OnLoad;
bi.UriSource = new Uri(imageFilePath, UriKind.RelativeOrAbsolute);
bi.EndInit();
return bi;
}
Alte URL:
/snippet/bitmapimage-von-datei-in-arbeitsspeicher-laden/5960
Eine schlechte Bewertung ist das eine, nicht zu sagen warum das andere…ist es denn so schwer ein Kommentar zu hinterlassen?