//看照片 public static List <PhotoData> PngToBase64(string photoId) { try { string PngFileName = path + "\\" + _importProjectName + "\\Photos\\" + photoId; if (!Directory.Exists(PngFileName)) { Directory.CreateDirectory(PngFileName); } string[] _photoPathList; string[] _photoNameList; _photoPathList = Directory.GetFiles(PngFileName); _photoNameList = Directory.GetFiles(PngFileName); List <PhotoData> AllPhoto = new List <PhotoData>(); int n = _photoPathList.Length; for (int i = 0; i < n; i++) { int m = _photoNameList[i].LastIndexOf("\\") + 1; _photoNameList[i] = _photoNameList[i].Substring(m, _photoNameList[i].Length - m); } for (int i = 0; i < n; i++) { Bitmap bmp = new Bitmap(_photoPathList[i]); MemoryStream ms = new MemoryStream(); bmp.Save(ms, System.Drawing.Imaging.ImageFormat.Png); byte[] arr = new byte[ms.Length]; ms.Position = 0; ms.Read(arr, 0, (int)ms.Length); ms.Close(); bmp.Dispose(); string base64 = Convert.ToBase64String(arr); PhotoData photo = new PhotoData(); photo.PhotoId = _photoNameList[i]; photo.PhotoString = base64; AllPhoto.Add(photo); } pngtobase64state = true; return(AllPhoto); } catch (Exception) { pngtobase64state = false; return(null); } }
//照照片 public static bool Base64ToPng(string Base64Data) { try { PhotoData Data = JsonConvert.DeserializeObject <PhotoData>(Base64Data); byte[] ByteData = Convert.FromBase64String(Data.PhotoString); int n = Data.PhotoId.IndexOf("-"); string pointId = Data.PhotoId.Substring(0, n); string PngFileName = path + "\\" + _importProjectName + "\\Photos\\" + pointId; if (!Directory.Exists(PngFileName)) { Directory.CreateDirectory(PngFileName); } MemoryStream ms = new MemoryStream(ByteData); Bitmap bmp = new Bitmap(ms); bmp.Save(PngFileName + "\\" + Data.PhotoId + ".png", ImageFormat.Png); return(true); } catch (Exception) { return(false); } }