删除图像问题

时间:2012-10-26 17:06:50

标签: c# .net wpf

  

可能重复:
  C# 4.0 unlock image after creating BitmapImage

我有这段代码在WPF中创建一个Image文件。

var newimage = new System.Windows.Controls.Image
{
    Stretch = Stretch.Fill,
    StretchDirection = StretchDirection.Both,
    Width = Width,
    Height = Height
};

var logo2 = new BitmapImage();
logo2.BeginInit();
logo2.UriSource = uri;
logo2.EndInit();
newimage.Source = logo2;

在此之后,某些进程必须删除ol文件并创建一个新文件但我面临错误

  

“无法删除文件,因为它正由另一个进程使用”

我该怎么做才能解决这个问题?

谢谢!


P.S。

我使用以下方法删除文件:

try
{
    if (File.Exists(fileName))
    {
        File.Delete(fileName);
        Debug.WriteLine("FILE MANAGER: File " + fileName + " has been deleted.");
    }
    return true;
}

1 个答案:

答案 0 :(得分:1)

您需要使用:

var logo2 = new BitmapImage();
logo2.BeginInit();
logo2.CacheOption = BitmapCacheOption.OnLoad;
logo2.UriSource = uri;
logo2.EndInit();