获取照片
//获取当前时间,作为寻找文件夹名,例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);
}