Image.FromFile 方法锁住文件解决方法
2016-07-02 14:20
246 查看
Image.FromFile 一旦使用后,对应的文件在一直调用 其生成的Image对象被Disponse前都不会被解除锁定,这就造成了一个问题,就是在这个图形被解锁前无法对图像进行操作(比如删除,修改等操作).
//读取文件流
FileStream fileStream = new FileStream(iconPath, FileMode.Open, FileAccess.Read);
int byteLength = (int)fileStream.Length;
byte[] fileBytes = new byte[byteLength];
fileStream.Read(fileBytes, 0, byteLength);
//文件流关閉,文件解除锁定
fileStream.Close();
pictureBox1.Image = Image.FromStream(new MemoryStream(fileBytes));
因为FromStream方法参数应用的流必须一直保持打开,故代码中有一个文件流向MemeoryStream流的转换,从而可以关闭文件流,保持MemoryStream流的打开状态。
//读取文件流
FileStream fileStream = new FileStream(iconPath, FileMode.Open, FileAccess.Read);
int byteLength = (int)fileStream.Length;
byte[] fileBytes = new byte[byteLength];
fileStream.Read(fileBytes, 0, byteLength);
//文件流关閉,文件解除锁定
fileStream.Close();
pictureBox1.Image = Image.FromStream(new MemoryStream(fileBytes));
因为FromStream方法参数应用的流必须一直保持打开,故代码中有一个文件流向MemeoryStream流的转换,从而可以关闭文件流,保持MemoryStream流的打开状态。
相关文章推荐
- Linux shell 的 test 命令用法详解
- 插件开发教程--(一)
- Linux(Ubuntu)下MySQL的安装与配置
- Web.xml配置详解之context-param (加载spring的xml,然后初始化bean看的)
- 测试小卒子--APP--闪退原因总结
- nohup 守护进程
- 5 keepalived高可用ipvs(主备模式)
- 提高项目19-有序数组中插入数据
- 在AndroidStudio中自定义Gradle插件
- css 文本溢出时显示省略标记(...)
- Hibernate5.1 SessionFactory获取方法
- Media player控件常用属性
- ubuntu中netstat命令的用法
- 【莫比乌斯反演】HDU1695_GCD
- Xpath用法
- 老码农教你在 StackOverflow 上谈笑风生
- android TextView如何通过setCompoundDrawables调整文字和图片的位置(文字显示在图片内)?
- Android BitmapShader 实战 实现圆形、圆角图片——代码分析
- win10 64位 环境下安装mysql5.7及登陆失败总结教程
- C# 队列(Queue)