Copy of image file used by another process.

I am trying to create a window for editing user rights, there is an Image control in this window
When I selected the image file, it will appear in this Image control and copy this file to the folder with images, the first time everything is fine, but the second time it shows an error

"The process cannot access the file" C: \ 1.jpg "because it is being used by another process."

I think this is because my Image Control uses this file, so I don’t know what I can do

private void Select_Click(object sender, RoutedEventArgs e) { OpenFileDialog od = new OpenFileDialog(); if (od.ShowDialog() == true) { string imageLocal = @"C:/1.jpg"; File.Copy(od.FileName, imageLocal, true); image1.Source = new BitmapImage(new Uri(imageLocal)); } } 
+5
c # file process image wpf
Aug 10 '13 at 23:27
source share
3 answers

If you want to load and display an image and save the file into a file system (for example, reload it or move it to another directory), the Uri constructor will not work because (as you specify) the BitmapImage class hangs in the file descriptor.

Use this method instead ...

  private static BitmapImage ByStream(FileInfo info) { //http://social.msdn.microsoft.com/Forums/en-US/wpf/thread/dee7cb68-aca3-402b-b159-2de933f933f1 try { if (info.Exists) { // do this so that the image file can be moved in the file system BitmapImage result = new BitmapImage(); // Create new BitmapImage Stream stream = new MemoryStream(); // Create new MemoryStream Bitmap bitmap = new Bitmap(info.FullName); // Create new Bitmap (System.Drawing.Bitmap) from the existing image file (albumArtSource set to its path name) bitmap.Save(stream, System.Drawing.Imaging.ImageFormat.Png); // Save the loaded Bitmap into the MemoryStream - Png format was the only one I tried that didn't cause an error (tried Jpg, Bmp, MemoryBmp) bitmap.Dispose(); // Dispose bitmap so it releases the source image file result.BeginInit(); // Begin the BitmapImage initialisation result.StreamSource = stream; // Set the BitmapImage StreamSource to the MemoryStream containing the image result.EndInit(); // End the BitmapImage initialisation return result; // Finally, set the WPF Image component source to the BitmapImage } return null; } catch { return null; } } 

This method accepts FileInfo and returns a BitmapImage, which you can display and at the same time move to another directory or display it again.

A simpler method, copied from another answer below, is this:

 public static BitmapImage LoadBitmapImage(string fileName) { using (var stream = new FileStream(fileName, FileMode.Open)) { var bitmapImage = new BitmapImage(); bitmapImage.BeginInit(); bitmapImage.CacheOption = BitmapCacheOption.OnLoad; bitmapImage.StreamSource = stream; bitmapImage.EndInit(); bitmapImage.Freeze(); return bitmapImage; } } 
+5
Aug 10 '13 at 23:50
source share

The method below loads BitmapImage from a file and immediately closes the file after loading. Note that you must set the BitmapCacheOption.OnLoad flag when the source stream closes immediately after EndInit .

 public static BitmapImage LoadBitmapImage(string fileName) { using (var stream = new FileStream(fileName, FileMode.Open)) { var bitmapImage = new BitmapImage(); bitmapImage.BeginInit(); bitmapImage.CacheOption = BitmapCacheOption.OnLoad; bitmapImage.StreamSource = stream; bitmapImage.EndInit(); bitmapImage.Freeze(); // just in case you want to load the image in another thread return bitmapImage; } } 

This code will work for any image format supported by WPF. When you transfer the contents of the image file as a stream to the StreamSource property StreamSource WPF will automatically create the corresponding decoder.

+2
Aug 11 '13 at 5:38
source share

A very simple solution:

 System.GC.Collect(); System.GC.WaitForPendingFinalizers(); File.Copy(od.FileName, imageLocal, true); 
+1
Oct 20 '14 at 18:53
source share



All Articles