how-to-convert-image-to-base64-in-asp-net
public Image Base64ToImage(string base64String)
{
// Convert Base64 String to byte[]
byte[] imageBytes = Convert.FromBase64String(base64String);
MemoryStream ms = new MemoryStream(imageBytes, 0,
imageBytes.Length);
// Convert byte[] to Image
ms.Write(imageBytes, 0, imageBytes.Length);
Image image = Image.FromStream(ms, true);
return image;
}
//Upload images to service and save in local folder
public ActionResult ImageUpload(string image)
{
if (image != null)
{
try
{
var id = "";
string converted = image.Substring(image.IndexOf(",") + 1);
Image TrueImage = Base64ToImage(converted);
string directory = Server.MapPath("~/UploadedImages");
if (!Directory.Exists(directory))
{
Directory.CreateDirectory(directory);
}
string filename = Guid.NewGuid().ToString();
string assignGuid = filename + ".Jpeg";
string path = System.IO.Path.Combine(directory, assignGuid);
TrueImage.Save(path, System.Drawing.Imaging.ImageFormat.Jpeg);
var ResizeToBytesimg = ResizeToBytes(path);
var base64img = Convert_ImageTo_Base64(path);
imgpost obj = new imgpost();
if (Session["newobid"] == null)
{
id = Session["ID"].ToString();
}
else
{
id = Session["newobid"].ToString();
}
id = id.Replace("\"", "").Replace("\"", "");
obj.obsID = id;
obj.b64Photo = base64img;
var json = new JavaScriptSerializer().Serialize(obj);
string WebServiceURL = "http://serviceinstallapp/Service1.svc/sendphotobytes";
return Json(PostDataToService(json, WebServiceURL));
}
catch (Exception ex)
{
return RedirectToAction("ValveInfo", "Home");
}
}
else
return RedirectToAction("ValveInfo", "Home");
}
public byte[] ResizeToBytes(string filename)
{
Bitmap img = new Bitmap(filename);
double oldwidth = img.Width;
double oldheight = img.Height;
double newheight;
double newwidth;
if (oldwidth > oldheight)
{
newwidth = 765;
newheight = 765 * (oldheight / oldwidth);
}
else
{
newheight = 765;
newwidth = 765 * (oldwidth / oldheight);
}
Bitmap imgout = new Bitmap(img, (int)newwidth, (int)newheight);
img.Dispose();
imgout.Save(filename, System.Drawing.Imaging.ImageFormat.Jpeg);
imgout.Dispose();
FileStream f = new FileStream(filename, FileMode.Open, FileAccess.Read, FileShare.Read);
int size = (int)f.Length;
byte[] MyData = new byte[f.Length + 1];
f.Read(MyData, 0, size);
f.Close();
return MyData;
}
//To convert Image To Base64 Format
public string Convert_ImageTo_Base64(string path)
{
string filename = path;
FileStream f = new FileStream(filename, FileMode.Open, FileAccess.Read, FileShare.Read);
int size = (int)f.Length;
byte[] MyData = new byte[f.Length + 1];
f.Read(MyData, 0, size);
f.Close();
return Convert.ToBase64String(MyData);
}
//To convert Jsondatetime/Date(1389435240000+0000)/ to date
public static DateTime ConvertToDate(string date)
{
DateTime val = new DateTime();
try
{
if (!string.IsNullOrEmpty(date))
{
date = date.Replace("/Date(", string.Empty).Replace(")/", string.Empty);
int pIndex = date.IndexOf("-");
if (pIndex < 0) pIndex = date.IndexOf("-");
long millisec = 0;
date = date.Remove(pIndex);
long.TryParse(date, out millisec);
System.Globalization.CultureInfo ci = new System.Globalization.CultureInfo("en-GB");
DateTime newDate = DateTime.Parse("1970,1,1);
val = newDate.AddMilliseconds(millisec);
}
}
catch
{
val = new DateTime();
}
return val;
}