mardi 14 avril 2015

Wpf Image Control blocks the file

I have a simple Window with button and second Window is opened when I click on the Button. Second Window has a Image control, which displays a .png-file. So if I use FileObject property for Binding all is OK, I can delete file from File Explorer. But if I use FileName property for Binding I cannot delete file from File Explorer, I get OS exception. I cannot do this even if I close second window, even if I invoke GC explicitly. What is the problem with FileName property? Any ideas?


Win 7, Net 4.0


Window1



<Grid>
<Button Content="Ok"
Width="100"
VerticalAlignment="Center"
HorizontalAlignment="Center"
Click="Click"
Padding="0,2,0,2"
IsDefault="True"
Name="_btnOk"/>
</Grid>



public partial class Window : Window
{

public Window()
{
InitializeComponent();
DataContext = this;
}

private void Click(Object sender, RoutedEventArgs e)
{
var window = new Window3();
window.ShowDialog();
}
}


Window2



<Grid>
<Image Source="{Binding FileObject}"></Image>
</Grid>

public partial class Window2 : Window
{
public Window2()
{
InitializeComponent();
DataContext = this;
FileName = "D:/pdf/myfile.png";

Closing += Window2_Closing;
}

public String FileName { get; set; }

public Object FileObject
{
get
{
if (String.IsNullOrEmpty(FileName))
return null;

if (!File.Exists(FileName))
return null;

var ms = new MemoryStream();
var bi = new BitmapImage();

using (var fs = new FileStream(FileName, FileMode.Open, FileAccess.Read))
{
fs.CopyTo(ms);
bi.BeginInit();
bi.StreamSource = ms;
bi.EndInit();
}
return bi;
}
}

void Window2_Closing(Object sender, System.ComponentModel.CancelEventArgs e)
{
GC.Collect();
GC.WaitForPendingFinalizers();
}
}

Aucun commentaire:

Enregistrer un commentaire