图像文件副本正在由另一个进程使用 [英] Image file copy, is being used by another process

查看:221
本文介绍了图像文件副本正在由另一个进程使用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I'm trying create a user perfil edit window, in this window has a Image control
When I selected a image file, it will show in this Image control and copy this file at my image folder, first time is all right, but second time, it show a error

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

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

我认为这是因为我的图像控件正在使用这个文件,所以我不知道该怎么做

I think it is because my Image control is using this file, so, I don't know what can I 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));
    }
}


推荐答案

您要加载并显示图像,并保持文件适合于文件系统中的操作(如重新加载或将其移动到另一个目录),则Uri构造函数将无法正常工作,因为(如您所指出的),BitmapImage类挂起对于文件句柄。

If you want to load and display an image, and keep the file amenable to operations in the file system (like reloading it or moving it to another directory), the Uri constructor will not work because (as you point out), the BitmapImage class hangs on to the file handle.

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

Instead, use a method like this...

    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可以显示并同时移动到另一个目录或再次显示。

This method takes a FileInfo and returns a BitmapImage which you can display and simultaneously move it to another directory or display it again.

从下面的另一个答案复制的一个更简单的方法是:

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

这篇关于图像文件副本正在由另一个进程使用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆