c#图片base64去转义字符_C# 图片转base64编码字符串,base64编码字符串转图片

///

/// 根据图片得到图片的base64编码字符串

///

/// 图片路径

/// base64字符串

public string GetBase64FromImage(string imagefile)

{

string strbaser64 = "";

try

{

string path = HttpContext.Current.Server.MapPath(imagefile);//根据虚拟路劲得到路片所在的物理路径

Bitmap bmp = new Bitmap(path);

MemoryStream ms = new MemoryStream();

bmp.Save(ms, System.Drawing.Imaging.ImageFormat.Jpeg);

byte[] arr = new byte[ms.Length];

ms.Position = 0;

ms.Read(arr, 0, (int)ms.Length);

ms.Close();

strbaser64 = Convert.ToBase64String(arr);

}

catch (Exception)

{

throw new Exception("Something wrong during convert!");

}

return strbaser64;

}

///

/// 保存图片并返回图片路径

///

/// base64编码字符串

/// 图片路径

public string SaveImage(string strImg)

{

string fpath = System.Web.HttpContext.Current.Server.MapPath("picture/");//图片存储文件夹路径

string time = DateTime.Now.ToString("yyyyMMddhhmmssfff");

string picturename = time; //文件名

String ExistsPath = fpath + "IDCardImage";

if (!Directory.Exists(ExistsPath))//查看存储路径的文件是否存在

{

Directory.CreateDirectory(ExistsPath); //创建文件夹,并上传文件

}

String newFilePath = fpath + "IDCardImage" + "/" + picturename; //文件保存路径

byte[] arr = Convert.FromBase64String(strImg);

MemoryStream ms = new MemoryStream(arr);

Bitmap bmp = new Bitmap(ms);

bmp.Save(newFilePath + ".jpg", System.Drawing.Imaging.ImageFormat.Jpeg); //保存为.jpg格式

ms.Close();

return picturename + ".jpg";

}

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值