Base64編碼儲存為圖片檔案
阿新 • • 發佈:2021-10-22
/// <summary> /// Base64編碼儲存為圖片檔案 /// </summary> /// <param name="base64Str">Base64編碼</param> /// <param name="savePath">圖片完整路徑</param> /// <returns></returns> public bool Base64StrToImage(string base64Str, string savePath) {var ret = true; try { base64Str = base64Str.Replace("data:image/png;base64,", "").Replace("data:image/jgp;base64,", "") .Replace("data:image/jpg;base64,", "").Replace("data:image/jpeg;base64,", ""); //將base64頭部資訊替換 var bytes = Convert.FromBase64String(base64Str);var dicPath = savePath.Substring(0,savePath.LastIndexOf('\\')); if (!Directory.Exists(dicPath)) { Directory.CreateDirectory(dicPath); } using (var imageFile = new FileStream(savePath, FileMode.Create)) { imageFile.Write(bytes,0, bytes.Length); imageFile.Flush(); } } catch (Exception ex) { ret = false; } return ret; }
此隨筆或為自己所寫、或為轉載於網路。僅用於個人收集及備忘。