12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364 |
- using System;
- using System.Drawing;
- using System.IO;
-
- namespace UnivateProperties_API.Containers.Property
- {
- public class ImageFormatter
- {
- public static string Base64ToImage(string binData, string path, string name)
- {
- string extention = ".jpg";
- if (binData.StartsWith("data:image/gif;base64,"))
- {
- extention = ".gif";
- binData = binData.Replace("data:image/gif;base64,", "");
- }
- if (binData.StartsWith("data:image/jpeg;base64,"))
- {
- extention = ".jpg";
- binData = binData.Replace("data:image/jpeg;base64,", "");
- }
- if (binData.StartsWith("data:image/png;base64,"))
- {
- extention = ".png";
- binData = binData.Replace("data:image/png;base64,", "");
- }
-
- if (!Directory.Exists(path))
- Directory.CreateDirectory(path);
-
- string filePath = string.Format(@"{0}\{1}{2}", path, name, extention);
- File.WriteAllBytes(filePath, Convert.FromBase64String(binData));
- return filePath;
- }
-
- public static string ImageToBase64(string Path)
- {
- if (File.Exists(Path))
- {
- using (Image image = Image.FromFile(Path))
- {
- using (MemoryStream m = new MemoryStream())
- {
- image.Save(m, image.RawFormat);
- byte[] imageBytes = m.ToArray();
-
- string base64String = Convert.ToBase64String(imageBytes);
-
- if (Path.EndsWith(".jpg") || Path.EndsWith(".jpeg"))
- base64String = "data:image/jpeg;base64," + base64String;
- if (Path.EndsWith(".gif"))
- base64String = "data:image/gif;base64," + base64String;
- if (base64String.EndsWith(".png"))
- base64String = "data:image/png;base64," + base64String;
-
- return base64String;
- }
- }
- }
- else
- return "";
- }
- }
- }
|