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

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

问题描述

我想创建一个用户PERFIL编辑窗口,在该窗口中有一个Image控件

当我选择一个图像文件,它会显示在此图像控制和这个文件在我的图像复制文件夹,第一次是不错,但是第二次,它显示一个错误



该进程无法访问该文件'C:\1.jpg',因为它是正在被另一个过程。



我想这是因为我的形象控制正在使用此文件,所以,我不知道我能做些什么

 私人无效Select_Click(对象发件人,RoutedEventArgs E)
{
打开文件对话框OD =新的OpenFileDialog();
如果(od.ShowDialog()==真)
{
串imageLocal = @C:/1.jpg
File.Copy(od.FileName,imageLocal,真正的);
image1.Source =新的BitmapImage(新的URI(imageLocal));
}
}


解决方案

如果要加载和显示图像,并保持文件服从操作在文件系统(如重装或将其移动到另一个目录),开放的构造不会工作,因为(正如你指出),在BitmapImage的类挂起到文件句柄。



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

 私有静态的BitmapImage ByStream(FileInfo的信息)
{//http://social.msdn.microsoft.com/Forums/en-US/wpf/thread/dee7cb68-aca3-402b-b159- 2de933f933f1
尝试
{
如果(info.Exists)
{
//为此,使得图像文件可以在文件系统
被移动BitmapImage的结果=新的BitmapImage();
//创建一个新的BitmapImage
流流=新的MemoryStream(); //创建新的MemoryStream
位图位图=新位图(info.FullName);
//创建一个从现有的图像文件
新位图(System.Drawing.Bitmap)(albumArtSource设置为路径名)
bitmap.Save(流System.Drawing.Imaging.ImageFormat .PNG);
//保存加载位图到的MemoryStream - PNG格式是唯一一个我
试过,没有导致错误(试JPG,BMP,MemoryBmp)
bitmap.Dispose() ; //处置位图,以便它释放源映像文件
result.BeginInit(); //开始了的BitmapImage的初始化
result.StreamSource =流;
//设置的BitmapImage的StreamSource的包含图像
result.EndInit将MemoryStream(); //结束的BitmapImage的初始化
返回结果; //最后,WPF图像组件的源设置为
的BitmapImage
}
返回NULL;
}

{
返回NULL;
}
}

这方法接受一个FileInfo并返回BitmapImage的你可以显示并同时将其移动到另一个目录,或再次显示。



另一种方法是...

 公共静态的BitmapImage LoadBitmapImage (字符串文件名)
{
使用(VAR流=新的FileStream(文件名,FileMode.Open))
{
变种的BitmapImage =新的BitmapImage();
bitmapImage.BeginInit();
bitmapImage.CacheOption = BitmapCacheOption.OnLoad;
bitmapImage.StreamSource =流;
bitmapImage.EndInit();
bitmapImage.Freeze();
返回BitmapImage的;
}
}


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

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

解决方案

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

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

An alternative method is...

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天全站免登陆