Laden...

Image Binding und anschließend wieder freigeben

Erstellt von nicky vor 7 Jahren Letzter Beitrag vor 7 Jahren 934 Views
N
nicky Themenstarter:in
232 Beiträge seit 2011
vor 7 Jahren
Image Binding und anschließend wieder freigeben

Hallo,

kann ich ein Bild, während es noch angezeigt wird, wieder freigeben und ggf. auch löschen?

Ich hab dazu folgenden Converter gefunden:

using System;
using System.Globalization;
using System.Windows.Data;
using System.Windows.Media.Imaging;

namespace Popcorn.Converters
{
    /// <summary>
    /// Used to check if the path to the image file is empty or not
    /// </summary>
    public class UriToCachedImageConverter : IValueConverter
    {
        /// <summary>
        /// Convert a path image to a bitmap-cached image
        /// </summary>
        /// <param name="value">The value produced by the binding source.</param>
        /// <param name="targetType">The type of the binding target property.</param>
        /// <param name="parameter">The converter parameter to use.</param>
        /// <param name="culture">The culture to use in the converter.</param>
        /// <returns>Cached image</returns>
        public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
        {
            if (string.IsNullOrEmpty(value?.ToString())) return null;
            var path = value.ToString();
            var bi = new BitmapImage();
            bi.BeginInit();
            bi.UriSource = new Uri(path);
            bi.CacheOption = BitmapCacheOption.OnLoad;
            bi.EndInit();
            return bi;
        }

        /// <summary>
        /// Not supported
        /// </summary>
        /// <param name="value">The value produced by the binding source.</param>
        /// <param name="targetType">The type of the binding target property.</param>
        /// <param name="parameter">The converter parameter to use.</param>
        /// <param name="culture">The culture to use in the converter.</param>
        public object ConvertBack(object value, Type targetType, object parameter,
            CultureInfo culture)
        {
            throw new NotSupportedException();
        }
    }
}

Leider scheint das nicht immer zu funktionieren. Ich erhalte trotzdem manchmal die Exception:

Fehlermeldung:
System.IO.IOException: The process cannot access the file '...Template\Images\Terminal.jpg' because it is being used by another process.

Jemand dazu eine Idee?

6.911 Beiträge seit 2009
vor 7 Jahren

Hallo nicky,

aus WPF-Sicht sollte es passen. Bist du sicher dass das Bild sonst nirgends im Code verwendet wird? Das kannst du z.B. auch mit dem Processexplorer (Handle suchen) prüfen.

Ansonsten gab es dieses Thema schon öfters. Forumssuche nach BitmapCacheOption

mfG Gü

Stellt fachliche Fragen bitte im Forum, damit von den Antworten alle profitieren. Daher beantworte ich solche Fragen nicht per PM.

"Alle sagten, das geht nicht! Dann kam einer, der wusste das nicht - und hat's gemacht!"