Reputation: 69
I store image into session in base64 format, how can I convert it into byte[]
I want to do something like this:
Byte[] bytes =(Byte[]) Session["picimg"];
it's not working.
This is my code:
StreamReader reader = new StreamReader(Request.InputStream);
String Data = Server.UrlDecode(reader.ReadToEnd());
reader.Close();
DateTime nm = DateTime.Now;
string date = nm.ToString("yyyymmddMMss");
//used date for creating Unique image name
Session["capturedImageURL"] = Server.MapPath("Userimages/") + date + ".jpeg";
Session["Imagename"] = date + ".jpeg";
// We can use name of image where ever we required that why we are storing in Session
drawimg(Data.Replace("imgBase64=data:image/png;base64,", String.Empty), Session["capturedImageURL"].ToString());
// it is method
// passing base64 string and string filename to Draw Image.
Session["picimg"] = (Data.Replace("imgBase64=data:image/png;base64,", String.Empty));
Byte[] bytes =(Byte[]) Session["picimg"];
System.IO.File.WriteAllBytes(@"Userimages", bytes);
Upvotes: 0
Views: 3681
Reputation: 540
You need the Convert.FromBase64String method to decode the base64 string into a byte array.
byte[] Bytes = Convert.FromBase64String(DataInBase64);
Upvotes: 1