C#图片文件共享问题

Image img=imageFromFile(sourceFileName);
 File.Delete(sourceFileName);

当从文件中读取图片后,再删除文件,出现文件正在使用,不能删除问题。

主要是读取文件后,资源没有得到释放。


        /// <summary>
        /// 获取图像
        /// </summary>
        /// <param name="fileName">文件名</param>
        /// <returns></returns>
        private Image GetImageFromFile(string fileName)
        {
            Image image = null;
            //得到图片的二进制码流
            using (FileStream inFs = new FileStream(fileName, FileMode.OpenOrCreate, FileAccess.Read))
            {
                byte[] buf = new byte[inFs.Length];
                BinaryReader br = new BinaryReader(inFs);
                br.Read(buf, 0, buf.Length);
                MemoryStream ms = new MemoryStream(buf);//创建流

                image = Image.FromStream(ms);//从流中创建image对象

                ms.Close();
                br.Close();
                inFs.Close();
                inFs.Dispose();//释放资源

            }

            return image;
        }
 

  • 8
    点赞
  • 8
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值