2

I'm retrieving image from database which as stored in base64 format. Here my requirement is, I want to convert a base64 image url into user friendly url. I tried many solutions, but I didn't get as i expected. Can anyone suggest me how to achieve this.

For Example :-

this is bas64

<img src="data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAAkAAAAJACAYAAABlmtk2AAAgAElEQVR4Xu 3dQYoj5paEUdng7Zhemie1ipz00rwfg2nSUOPKvEdCdPD11IqreucPQZCT/u3j4+PPPx6P3x//D//vn8 fj389/dv/+9zxe/u9x//mt+ecvAvVH9DybvxvKhU//3/734+N//vrx42859K7s57/987v797/nBfJ/j/ vPb80/fxGoP6Ln2fzdUC58+jeARBCz/QAQEOP5IyDG80dAjOePgBjPHwEx3gBCQI33A1BBy+dvfprOXw Utn7/5aTp/FbR8A8j8ON0PgAnpQP7Ex+H8mZAO5E98HM6fCelAA4j4PNwPwA3lQv6i59n83VAu5C96ns 3fDeVCA0j0npDtB/AERDiRP+A9IZr/ExDhRP6A94Ro/k9AhBMNIMB7RrQfwDMU7zfyv9s9I5n/MxTvN/ K/2z0jmf8zFO83GkB3u6ck+wE8hfF8JP8z3VOC+T+F8Xwk/zPdU4L5P4XxfKQBdKZ7TrAfwHMcr1fyv8 o9J5f/cxyvV/K/yj0nl/9zHK9XGkBXuSfl+gE8CfJ4Jv8j3JNi+T8J8ngm/yPck2L5PwnyeKYBdIR7Vq 6/oieZ/N3Q7mQv+h5dsG/AeQ9OF9YKFAD6Pz8HKw/TEgH8ic+DufPhHRgwb8BRBWw8EKBGkDWAUnXH9H zbP5uKBfyFz3PLvg3gLwH5wsLBWoAnZ+fg/WHCelA/sTH4fyZkA4s+P/28fHx5x+Px+8k8abwP4/Hv59 f3b//PQ+Q/3vcf35r/vmLQP0RPc/m74Zy4dP//wBHqel8UTBFDwAAAABJRU5ErkJggg==" />

i want to get instead of base64

<img src="http://www.domainname.com/imagename.jpg />

how to achieve this any suggestion

Sri
  • 174
  • 2
  • 14
  • Possible duplicate of [converting a base 64 string to an image and saving it](http://stackoverflow.com/questions/5400173/converting-a-base-64-string-to-an-image-and-saving-it) – Prabhat Sinha Apr 29 '16 at 05:37

2 Answers2

0

he Base64 string parameter is first converted to a byte array by invoking the Convert.FromBase64String method. Next we create a MemoryStream against the resulting byte array, which serves as a parameter to the Bitmap class’ FromStream static method.

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;
}
Prabhat Sinha
  • 1,500
  • 20
  • 32
  • getting error on this line : Image image = Image.FromStream(ms, true); Error : FromStream not a defination of Image – Sri Apr 29 '16 at 06:29
  • In ASP.Net, you need to have a System.Web.UI.WebControls.Image in your web form and assign the ImageUrl property the URL of the image to be displayed. – Prabhat Sinha Apr 29 '16 at 06:34
  • can you provide me any example related to issue, because its not working for me – Sri Apr 29 '16 at 06:56
0

For this to work you will need to have an identifier of the image like unique id or name.

so create a action under YourController

public void Image(string id){
    var base64String="";// get from id
    byte[] bytes = Convert.FromBase64String(base64String);

    Image image;
    Response.ContentType = "image/jpeg";
    using (MemoryStream ms = new MemoryStream(bytes))
    {
        image = Image.FromStream(ms);
        image.Save(Response.OutputStream, System.Drawing.Imaging.ImageFormat.Jpeg);
    }
}

now on RouteConfig.cs add new route to top of the routes

routes.MapRoute(
                name: "ImageRoute",
                url: "{id}.jpg",
                defaults: new { controller = "YourController", action = "Image" }
            );

However i would suggest not to use url: "{id}.jpg" as it can easily mess up any other physical images you have in your directory. url: "{controller}/{action}/{id}.jpg"

Now according to your Route you can use

<img src="http://www.domainname.com/[id/name].jpg />

or

<img src="http://www.domainname.com/YourController/Image/[id/name].jpg />

Ruchan
  • 3,124
  • 7
  • 36
  • 72