从文件紧密结合C#图片 [英] c# Image from file close connection

查看:107
本文介绍了从文件紧密结合C#图片的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用C#在PictureBox显示图像。然后我想改变形象的名字。我不能,因为我得到的图像正被另一个进程。

I display an image using c# in a pictureBox. Then I want to change the image's name. I can't because I get that the image is being used by another process.

我打开这个图片的方式

 Image image1 = Image.FromFile("IMAGE LOCATION"); ;
        pictureBox1.Image = image1; 



然后试图改变它的名字就这样,我得到IO异常说:该进程无法访问文件,因为它正由另一个进程。

Then try to change it's name this way and I get an IO exception saying " The process cannot access the file because it's being used by another process.

System.IO.File.Copy(@"OldName", @"NewName"); //copy changes name if paths are in the same folder

是不是对象持此搜索形象?为何由以前的过程中仍然锁定的文件吗?任何建议,将不胜感激。非常感谢!

Isn't the object image1 holding the image? Why is the file still locked by the previous process? Any suggestions would be appreciated. Many thanks!

推荐答案

有看这个问题:被新的位图(文件路径)锁定免费文件

要自由,你需要做位一个副本锁定:

To free the lock you need to make a copy of the bits:

Image img;
using (var bmpTemp = new Bitmap("image_file_path"))
{
     img = new Bitmap(bmpTemp);
}

这篇关于从文件紧密结合C#图片的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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