Cannot delete file used by some other process

前端 未结 2 506
盖世英雄少女心
盖世英雄少女心 2020-11-27 08:40

I am displaying some image in my wpf app using following code:

 

        
相关标签:
2条回答
  • 2020-11-27 09:06

    In order to be able to delete the image while it is displayed in an ImageControl, you have to create a new BitmapImage or BitmapFrame object that has BitmapCacheOption.OnLoad set. The bitmap will then be loaded from file immediately and the file is not locked afterwards.

    Change your property from string TemplateImagePath to ImageSource TemplateImage and bind like this:

    <Image Source="{Binding TemplateImage}"/>
    

    The set the TemplateImage property like this:

    BitmapImage image = new BitmapImage();
    image.BeginInit();
    image.CacheOption = BitmapCacheOption.OnLoad;
    image.UriSource = new Uri(item.FullName);
    image.EndInit();
    TemplateImage = image;
    

    or this:

    TemplateImage = BitmapFrame.Create(
        new Uri(item.FullName),
        BitmapCreateOptions.None,
        BitmapCacheOption.OnLoad);
    

    If you want to keep binding to your TemplateImagePath property you may instead use a binding converter that converts the string to an ImageSource as shown above.

    0 讨论(0)
  • 2020-11-27 09:09

    According to Clemens suggestion, here is the binding converter to have a good code-reuse:

    namespace Controls
    {
        [ValueConversion(typeof(String), typeof(ImageSource))]
        public class StringToImageSourceConverter : IValueConverter
        {
            public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
            {
                if (!(value is string valueString))
                {
                    return null;
                }
                try
                {
                    ImageSource image = BitmapFrame.Create(new Uri(valueString), BitmapCreateOptions.IgnoreImageCache, BitmapCacheOption.OnLoad);
                    return image;
                }
                catch { return null; }
            }
    
            public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture)
            {
                throw new NotImplementedException();
            }
        }
    }
    

    And there is a string for binding, for example

    public string MyImageString { get; set; } = @"C:\test.jpg"
    

    And in the UI the converter is used, in my case from the Library named "Controls"

    <Window x:Class="MainFrame"
            xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
            xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
            xmlns:controls="clr-namespace:Controls;assembly=Controls">
        <Window.Resources>
            <controls:StringToImageSourceConverter x:Key="StringToImageSourceConverter" />
        </Window.Resources>
        <Grid>
            <Image Source="{Binding MyImageString, Converter={StaticResource StringToImageSourceConverter}}" />
        </Grid>
    </Window>
    
    0 讨论(0)
提交回复
热议问题