另一个进程正在使用图像文件副本

本文关键字:图像 文件 副本 进程 另一个 | 更新日期: 2023-09-27 18:27:06

我正在尝试创建一个用户perfil编辑窗口,该窗口中有一个Image控件
当我选择一个图像文件时,它会显示在这个图像控件中,并将此文件复制到我的图像文件夹中,第一次可以,但第二次,它显示错误

"进程无法访问文件C:''1.jpg,因为它正被另一个进程使用。"

我想这是因为我的图像控制正在使用这个文件,所以,我不知道我能做什么

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));
    }
}

另一个进程正在使用图像文件副本

如果您想加载和显示图像,并使文件服从文件系统中的操作(如重新加载或将其移动到另一个目录),Uri构造函数将不起作用,因为(正如您所指出的)BitmapImage类挂在文件句柄上。

相反,使用这样的方法。。。

    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's initialisation   
                result.StreamSource = stream;
                // Set the BitmapImage's StreamSource to the MemoryStream containing the image   
                result.EndInit(); // End the BitmapImage's initialisation   
                return result; // Finally, set the WPF Image component's source to the 
                                BitmapImage  
            }
            return null;
        }
        catch
        {
            return null;
        }
    }

此方法获取一个FileInfo并返回一个BitmapImage,您可以显示它,同时将它移动到另一个目录或再次显示它。

一个简单得多的方法,复制自下面的另一个答案,是这样的:

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;
    }
}

下面显示的方法从文件加载BitmapImage,并在加载后立即关闭文件。注意,当源流紧接在EndInit之后关闭时,有必要设置BitmapCacheOption.OnLoad标志。

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;
    }
}

此代码适用于WPF支持的任何图像格式。当将图像文件内容作为流传递给StreamSource属性时,WPF将自动创建适当的解码器。

非常简单的解决方案:

System.GC.Collect();  
System.GC.WaitForPendingFinalizers();
File.Copy(od.FileName, imageLocal, true);