Click here to Skip to main content
15,069,838 members
Please Sign up or sign in to vote.
4.50/5 (3 votes)
See more:
I have an image control with a source image located in my c drive. I get a message that the image is being used by another process whenever I try to delete the original image to change it with another one dynamically. How do I release the image from the image control to be able to delete it.

I tried this variants:
C#
string path = ((BitmapImage)img.Source).UriSource.LocalPath;
img.SetValue(System.Windows.Controls.Image.SourceProperty, null);
File.Delete(path);

And:
C#
string path = ((BitmapImage)img.Source).UriSource.LocalPath;
img.Source = null;
File.Delete(path)


But it's not work...

This code produces the following error when the DeleteImage method is called:

The process cannot access the file 'C:\Picther.jpg' because it is being used by another process.
Posted
Updated 25-May-12 7:26am
v4
Comments
El_Codero 25-May-12 12:51pm
   
Please improve your question! What do you mean with "when we using it"? How are this images stored? As Resource or filepath to hdd?
Member 3232874 25-May-12 13:09pm
   
string path = ((BitmapImage)img.Source).UriSource.LocalPath;
img.SetValue(System.Windows.Controls.Image.SourceProperty, null);
File.Delete(path);

Error:
The process cannot access the file 'C:\Picther.jpg' because it is being used by another process.

And:
string path = ((BitmapImage)img.Source).UriSource.LocalPath;
img.Source = null;
File.Delete(path)

Hi,

it's a bit tricky to achieve this because BitmapStream from BitmapImage couldn't be disposed by ImageControl.
BitmapCacheOption.OnLoad Property solves this problem, like this:

C#
public MainWindow()
      {
          InitializeComponent();
           //Display your image in Image Control
          image1.Source = BitmapFromUri(new Uri(@"c:\test.jpg"));
      }

      private void button_DEL_Image_Click(object sender, RoutedEventArgs e)
      {
          image1.Source = null;
          File.Delete(@"c:\test.jpg");
      }

      public static ImageSource BitmapFromUri(Uri source)
      {
          var bitmap = new BitmapImage();
          bitmap.BeginInit();
          bitmap.UriSource = source;
          bitmap.CacheOption = BitmapCacheOption.OnLoad;
          bitmap.EndInit();
          return bitmap;
      }


Regards
   
v2
Comments
El_Codero 25-May-12 14:08pm
   
yes, tried it a few minutes ago in my enviroment and it works. What exactly doesn't work?
Shahin Khorshidnia 25-May-12 14:12pm
   
Sorry I tried it again and it worked. ( mistake in copy&pasting code ;) )
My +5
El_Codero 25-May-12 14:30pm
   
Glad it works for you now. Thanks for upvoting, but who has downvoted??
Shahin Khorshidnia 26-May-12 0:04am
   
I don't know who?! But I think he/she was not a platinum. By the way I've upvoted and now, I also clicked on bookmark for this solution.
El_Codero 26-May-12 16:45pm
   
No problem with downvoting, but a reply like "because of..." would be nice. Thanks for upvoting and bookmarking Shahin ;) Regards
VJ Reddy 31-May-12 20:09pm
   
Good answer. 5!
El_Codero 1-Jun-12 18:04pm
   
Thank you Reddy!
Hamza AL-Shlafi 31-May-20 10:04am
   
Thank you very much ,This solution is success
Hello

Try this:

C#
img.Source = null;
(new System.Threading.Thread(() =>
 {
     while (true)
     {
         try
         {
             File.Delete(path);
             MessageBox.Show("Picture Removed");
             break;
         }
         catch{}         
     }
 })).Start();


You can show a ProgressBar while tring to remove, that is showing: "Please wait to remove the file..."
   
v3
I think what you are trying to do is change the image dynamically. You can read image to memory, and use that instead of the actual file. Read the file into memory, use it as the source for the image, and then do any changes to the original file:

Bitmap bitmap = new Bitmap(@"C:\temp\FoxLogo.png");
IntPtr hBitmap = bitmap.GetHbitmap();
ImageSource wpfBitmap = Imaging.CreateBitmapSourceFromHBitmap(hBitmap, 	IntPtr.Zero, Int32Rect.Empty, BitmapSizeOptions.FromEmptyOptions()); ;
	img.Source = wpfBitmap;		
bitmap.Dispose();


May be a better way to do this.
   
Here is another way I found to load an image into memory and use it as an image source:


BitmapImage image = new BitmapImage();
image.BeginInit();
Uri imageSource=new Uri("file://"+ "C:/temp/FoxLogo.png");
image.UriSource = imageSource;
image.EndInit();
img.Source = image;
   
Comments
Spencer Kittleson 31-May-14 17:31pm
   
This solution works great. Here is a image object held in memory based solution as well.

//convert bitmap to bitmapimage
using (var memory = new MemoryStream())
{
image.Save(memory, ImageFormat.Png);
memory.Position = 0;

var bitmapImage = new BitmapImage();
bitmapImage.BeginInit();
bitmapImage.StreamSource = memory;
bitmapImage.CacheOption = BitmapCacheOption.OnLoad; // this is key to destory the image with a new image coming in.
bitmapImage.EndInit();

wpfPictureBox.Source = bitmapImage;
}

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)




CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900