locked
Convert Image to Base64 RRS feed

  • Question

  • User145715640 posted

    Hi,

    I can able to convert a string to base64 but how to convert a Image to Base64 in MVC and vise versa

    Tuesday, January 13, 2015 4:32 AM

Answers

  • User145715640 posted

    Got the solution:

     public ActionResult ConvertImageToBase64(baseImage img111)
            {
                string base64String = string.Empty;
                // Convert Image to Base64
                using (var img = System.Drawing.Image.FromFile(img111.Image)) // Image Path from File Upload Controller
                {
                    using (var memStream = new MemoryStream())
                    {
                        img.Save(memStream, img.RawFormat);
                        byte[] imageBytes = memStream.ToArray();
    
                        // Convert byte[] to Base64 String
                         base64String = Convert.ToBase64String(imageBytes);
    
                        ViewBag.Image64 = base64String;
                     //   return base64String;
                    }
                }
    
        //        at View side
        //<img src="data:image/jpg;base64,@ViewBag.Image64" width="80" height="80"/>
    
    or
    
                Base64StringToImage(base64String);
                // Convert Base64 to Image 
      
                 
    
                return View();
            }
    
            public System.Drawing.Image Base64StringToImage(string base64String)
            {
                byte[] imageBytes = Convert.FromBase64String(base64String);
                var memStream = new MemoryStream(imageBytes, 0, imageBytes.Length);
    
                memStream.Write(imageBytes, 0, imageBytes.Length);
                var image = System.Drawing.Image.FromStream(memStream);
                return image;
            }



    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Tuesday, January 13, 2015 5:13 AM

All replies

  • User145715640 posted

    Got the solution:

     public ActionResult ConvertImageToBase64(baseImage img111)
            {
                string base64String = string.Empty;
                // Convert Image to Base64
                using (var img = System.Drawing.Image.FromFile(img111.Image)) // Image Path from File Upload Controller
                {
                    using (var memStream = new MemoryStream())
                    {
                        img.Save(memStream, img.RawFormat);
                        byte[] imageBytes = memStream.ToArray();
    
                        // Convert byte[] to Base64 String
                         base64String = Convert.ToBase64String(imageBytes);
    
                        ViewBag.Image64 = base64String;
                     //   return base64String;
                    }
                }
    
        //        at View side
        //<img src="data:image/jpg;base64,@ViewBag.Image64" width="80" height="80"/>
    
    or
    
                Base64StringToImage(base64String);
                // Convert Base64 to Image 
      
                 
    
                return View();
            }
    
            public System.Drawing.Image Base64StringToImage(string base64String)
            {
                byte[] imageBytes = Convert.FromBase64String(base64String);
                var memStream = new MemoryStream(imageBytes, 0, imageBytes.Length);
    
                memStream.Write(imageBytes, 0, imageBytes.Length);
                var image = System.Drawing.Image.FromStream(memStream);
                return image;
            }



    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Tuesday, January 13, 2015 5:13 AM