C#Framework获取本地图片并转为Base64格式

获取照片

 //获取当前时间,作为寻找文件夹名,例2022-03-25
            string times = DateTime.Now.ToString("yyyy-MM-dd");
            //寻找主程序当前路径下的xxxx-xx-xx文件夹
            string path = AppDomain.CurrentDomain.BaseDirectory + times;
            //Base64方法
            FileToBase64 fileToBase64 = new FileToBase64();
            //判定当前目录下是否有文件夹
            if (Directory.Exists(path))
            {
                //获取文件夹信息
                DirectoryInfo foder = new DirectoryInfo(path);
                //接受数据的实体
                var date = new YXZX_School_UserInfo();
                //获取当前目录下的所有照片
                FileInfo[] files = foder.GetFiles("*.jpg");

Base64格式转化代码

 public string FileToBase64String(string Porho)
        {
            FileStream fsForRead = new FileStream(Porho, FileMode.Open);//文件路径
            string base64Str = "";
            try
            {
                //读写指针移到距开头10个字节处
                fsForRead.Seek(0, SeekOrigin.Begin);
                byte[] bs = new byte[fsForRead.Length];
                int log = Convert.ToInt32(fsForRead.Length);
                //从文件中读取10个字节放到数组bs中
                fsForRead.Read(bs, 0, log);
                base64Str = Convert.ToBase64String(bs);
                return base64Str;
            }
            catch (Exception ex)
            {
                Console.Write(ex.Message);
                Console.ReadLine();
                return base64Str;
            }
            finally
            {
                fsForRead.Close();
            }

        }

多张照片转化

  for (int i = 0; i < files.Length; i++)
                {
                    //将读取到的照片转化为Base64格式
                    date.Photo = fileToBase64.FileToBase64String(files[i].FullName);
                    //获取当前照片的身份证号
                    date.Code = string.Join(",", files[i].Name.Substring(0, 18));
                    //存储
                    YXZX_School_UserInfoBLL.StudentPhoto(date);
                }

  • 0
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值