find and solve || findandsolve.com
Please wait.....

.net core image processing base64 string

.net core image processing base64 string

Base64 is a group of binary-to-text encoding schemes which is represent binary data in an ASCII string format by translating it into a radix-64 representation.
This is encoding commonly used when  required to convert binary data to string format.This is use commonly use in number of applications,email via MIME etc.

Convert Image to Base64string Example

Create function 'base64String' as given below
    public static string ImageToBase64(Image image, System.Drawing.Imaging.ImageFormat format)
        {
            using (MemoryStream ms = new MemoryStream())
            {
                //Convert Image to byte[]
                image.Save(ms, format);
                byte[] imageBytes = ms.ToArray();
                //Convert byte[] to Base64 String
                string base64String = Convert.ToBase64String(imageBytes);
                return base64String;
            }
        }
 
After that, call this 'ImageToBase64' from your controller as given below

 public ActionResult Create(HttpPostedFileBase file)
{
      //convert uploaded image as image object like given below
       Image image= Image.FromStream(file.InputStream, true, true);
       //call 'ImageToBase64' function here
       string base64String = ImageToBase64(image, System.Drawing.Imaging.ImageFormat.Jpeg);
      //'System.Drawing.Imaging.ImageFormat.Jpeg' is the image extension
       return View();
}

Convert Base64string to Image Example

Create function 'Base64ToImage' as given below
     public static 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;
        }
 
After that, call this 'Base64ToImage' from your controller as given below
public ActionResult Create(string base64String)
{
      //call this function as give below
       Image image= Base64ToImage(base64String);
       return View();
}

Sundar  Neupane

Sundar Neupane

I like working on projects with a team that cares about creating beautiful and usable interfaces.

Comments



Report Response