开发者

Delete image file used by XAML

开发者 https://www.devze.com 2022-12-25 16:55 出处:网络
I\'m t开发者_如何学编程rying to delete a Image file in WPF, but WPF locks the file. <Image Source=\"C:\\person.gif\" x:Name=\"PersonImage\">

I'm t开发者_如何学编程rying to delete a Image file in WPF, but WPF locks the file.

    <Image Source="C:\person.gif" x:Name="PersonImage">
        <Image.ContextMenu>
            <ContextMenu>
                <MenuItem Header="Delete..." x:Name="DeletePersonImageMenuItem" Click="DeletePersonImageMenuItem_Click"/>
            </ContextMenu>
        </Image.ContextMenu>
    </Image>

And the Click handler just looks like this:

    private void DeletePersonImageMenuItem_Click(object sender, RoutedEventArgs e)
    {
        System.IO.File.Delete(@"C:\person.gif");
    }

But, when I try to delete the file it is locked and cannot be removed.

Any tips on how to delete the file?


My application Intuipic deals with this by using a custom converter that frees the image resource. See the code here.


Using code behind, you can use the cache option BitmapCacheOption.OnLoad:

                BitmapImage bi = new BitmapImage();
                bi.BeginInit();
                bi.CacheOption = BitmapCacheOption.OnLoad;
                bi.CreateOptions = BitmapCreateOptions.IgnoreImageCache;
                bi.UriSource = new Uri(PathToImage);
                bi.EndInit();
                PersonImage.Source = bi;

Because I struggled to find it, I will add that if you want to replace the deleted image dynamically, you need to add the argument BitmapCreateOptions.IgnoreImageCache.


First Remove it from the PersonImage control then delete the image. Hope that will help. As you have assigned to the control in source, and remove it without unassigning the control source.

PersonImage.Source = null; 
System.IO.File.Delete(@"C:\person.gif"); 

hope that will help.


The most easiest way to do this will be, creating a temporary copy of your image file and using it as a source.. and then at end of your app, deleting all temp files..

static List<string> tmpFiles = new List<string>();

static string GetTempCopy(string src)
{
   string copy = Path.GetTempFileName();
   File.Copy(src, copy);
   tmpFiles.Add(copy);
   return copy;
}

static void DeleteAllTempFiles()
{
   foreach(string file in tmpFiles)
   {
      File.Delete(file);
   }
}

Image caching in WPF also can be configured to do this, but for some reason my various attempts failed and we get unexpected behaviour like not being able to delete or refresh the image etc, so we did this way.


Do not attach the physical file to the object.

BitmapImage bmp;
static int filename = 0;
static string imgpath = "";


private void saveButton_Click(object sender, RoutedEventArgs e)
{
  try
  {
    filename = filename + 1;
    string locimagestored = Directory.GetParent(Assembly.GetExecutingAssembly().Location).ToString();
    locimagestored = locimagestored + "\\StoredImage\\";
    imgpath = locimagestored + filename + ".png";
    webCameraControl.GetCurrentImage().Save(imgpath);

    Bitmap bitmap = new Bitmap(@imgpath);
    IntPtr hBitmap = bitmap.GetHbitmap();
    ImageSource wpfBitmap = Imaging.CreateBitmapSourceFromHBitmap(hBitmap, IntPtr.Zero, Int32Rect.Empty,    BitmapSizeOptions.FromEmptyOptions()); ;

    if (filename == 1)
    {
      imgSavedOnline0.Source = wpfBitmap;
      bitmap.Dispose();
    }
    else if (filename == 2)
    {
      imgSavedOnline1.Source = wpfBitmap;
      bitmap.Dispose();
    }
    else if (filename == 3)
    {
      imgSavedOnline2.Source = wpfBitmap;
      bitmap.Dispose();
    }
    else if (filename == 4)
    {
      imgSavedOnline3.Source = wpfBitmap;
      bitmap.Dispose();
    }

    System.IO.DirectoryInfo di2 = new DirectoryInfo(locimagestored);

    foreach (FileInfo file in di2.GetFiles())
    {
      file.Delete();
    }    
  }
  catch (Exception ex)
  {
    textBox1.Text = ex.Message;
  }
}
0

精彩评论

暂无评论...
验证码 换一张
取 消