//fileName 图片名
// dirPath 存放路劲
//base64String 图片 base64string ,记得去除 data:image/jpg;base64, 字符串
public Void SaveBase64StringToImg(string base64String, string fileName, string dirPath){
if (!Directory.Exists(dirPath))
Directory.CreateDirectory(dirPath);
if (!string.IsNullOrEmpty(base64String))
{
string dummyData = base64String.Trim().Replace("%", "").Replace(",", "").Replace(" ", "+");
if (dummyData.Length % 4 > 0)
{
dummyData = dummyData.PadRight(dummyData.Length + 4 - dummyData.Length % 4, '=');
}
if (!dirPath.EndsWith("/"))
dirPath = dirPath + "/";
string imgReallyPath = dirPath + fileName;
if (File.Exists(imgReallyPath))
File.Delete(imgReallyPath);
//获取base64字符串
byte[] imgBytes = Convert.FromBase64String(dummyData);
//将base64字符串转换为字节数组
System.IO.Stream stream = new System.IO.MemoryStream(imgBytes);
//将字节数组转换为字节流
//将流转回Image,用于将PNG 式照片转为jpg,压缩体积以便保存。
System.Drawing.Image image = System.Drawing.Image.FromStream(stream);
image.Save(imgReallyPath, System.Drawing.Imaging.ImageFormat.Jpeg);//保存图片
stream.Close();
}
}