ElementFlow - release media

May 15, 2008 at 11:41 PM
I'm playing around with the ElementFlow example, and I've modified the StringCollection to get images from a folder.  The problem that I'm seeing is that if I remove the image from the StringCollection while running... the image doesn't get released.  I'm unable to remove the image from the filesystem.  How do I go about releasing the image after it's been removed from the StringCollection?  I'm assuming the file is still open, but I don't know how to close it.

Many thanks!
Coordinator
May 16, 2008 at 1:56 AM
That's a pretty interesting issue. I am not sure if WPF is maintaining some kind of lock over the images. I haven't encountered this issue so I am not sure if I have the answers. Can you try asking on the WPF Forums or on WPF Disciples groups ?
May 16, 2008 at 11:19 AM
Edited May 16, 2008 at 11:28 AM
Yes, WPF keep lock of images. The solution for this  is to implement a value converter and use it on binding...
here is an example:

    public class ImagePathConverter: IValueConverter
    {

        #region IValueConverter Members

        public object Convert(object value, System.Type targetType, object parameter, System.Globalization.CultureInfo culture)
        {
            string path = (string) value;
            BitmapImage image = new BitmapImage();
            image.DecodePixelHeight = 200;
            image.BeginInit();
            image.CacheOption = BitmapCacheOption.OnLoad;
            image.UriSource = new Uri(path);
            image.EndInit();
            return image;
        }

        public object ConvertBack(object value, System.Type targetType, object parameter, System.Globalization.CultureInfo culture)
        {
            throw new System.NotImplementedException();
        }

        #endregion
    }


And use like this:

xmlns:local="clr-namespace:ComicReaderX.Viewer.Converters"
<Window.Resources>
        <!--Conversores-->
        <local:ImagePathConverter x:Key="imageConv"/>
<Windows.Resources>

<Image Name="Image1" Source="{Binding Path = FileName, Converter={StaticResource imageConv}}" Stretch="None">

i hope this help you

May 16, 2008 at 5:08 PM
Thanks Quicoli!

I'll give that a shot later today.  Many thanks!  Needless to say... this is all quite new for me.

-Ricky
May 16, 2008 at 6:54 PM
Edited May 16, 2008 at 7:18 PM
Ok... I finally stumbled on getting it working within the XAML...  Needless to say... I think I should learn more about WPF.  I'm a lost puppy.

Here's what I did...

Window1.xaml -

    <DataTemplate x:Key="TestDataTemplate" DataType="{x:Type sys:String}">
        <DataTemplate.Resources>
            <local:ImagePathConverter x:Key="ImagePathConverter"/>
        </DataTemplate.Resources>
        <Border Background="White"
                                Padding="5"
                                BorderThickness="5"
                                BorderBrush="LightGray">
            <Image Source="{Binding Path=., Converter={StaticResource ImagePathConverter}}"
                                    Stretch="Fill" />
        </Border>
    </DataTemplate>

Thanks again Quicoli for the kickstart. :-)

 NOTE: If VS 2008 is open it also will lock the resources... in my scenario I have to test outside of VS.