0

Please, help me with following.

I have a WPF application. There are ListView with Images as items and one Image - DisplayedImage. When item clicked - its image displayed in the DisplayedImage Image Control.

When Image Control have some transformations I need to save them to source file.

How I can to free needed file, if it uses as source for ListView items and DisplayedImage Image Control?

Exception

The process cannot access the file "<Path>" because it is being used by another process

XAML

<Image 
    x:Name="DisplayedImage" 
    Source="{Binding Path=TheImageViewItem.DisplayedPath, IsAsync=True, Mode=OneWay}"/>

<ListView x:Name="listView" 
              ItemsSource="{Binding Path=Images, IsAsync=True}" />

C#

JpegBitmapEncoder jpegBitmapEncoder = new JpegBitmapEncoder();
jpegBitmapEncoder.QualityLevel = 100;
jpegBitmapEncoder.Frames.Add(BitmapFrame.Create(DisplayedImage.Source as BitmapSource));
if (File.Exists(file))
    File.Delete(file);
FileStream fileStream = new FileStream(file, FileMode.CreateNew);
jpegBitmapEncoder.Save(fileStream);
fileStream.Close();

THANKS TO ALL

  • As long as the image exist in the ListView with Images, it is kept open, and you cannot overwrite it. – wimh Jan 24 '16 at 12:07
  • Set the control to null which should free the image. – jdweng Jan 24 '16 at 12:12
  • You may change your Image.Source binding from string in TheImageViewItem.DisplayedPath to a BitmapImage instance. For example like here http://stackoverflow.com/a/20648/5574010 – Anton Danylov Jan 24 '16 at 12:29

0 Answers0