NoWar
NoWar

Reputation: 37632

Create Image based on JSON output

I need help to create set of images via JSON in ASP .NET MVC4.

Is it possible to do?

My working code is following and I have no idea how to integrate that functionality.

Thank you for any help!

[AcceptVerbs("POST")]
public JsonResult ShowUserImages(string id)
{
   var result = List<Bitmap>();

   // Create/Get Images and send it with JSON ???

   return Json(result, JsonRequestBehavior.AllowGet);
}

HTML

 $.post(url, function (data) {
         if (data) {
             // Can we create an IMAGE tag here using JSON output ???           

} else {
             alert('Sorry, there is some error.');
         }
     }); 

Upvotes: 1

Views: 5746

Answers (1)

NoWar
NoWar

Reputation: 37632

Solution is here http://www.codeproject.com/Articles/201767/Load-Base64-Images-using-jQuery-and-MVC

So in my case it will be like

[AcceptVerbs("POST")]
public JsonResult ShowUserImages(string id)
{
   var bitmap = GenerateBitmap(id);

   MemoryStream ms = new MemoryStream();
   bitmap .Save(ms, ImageFormat.Png);
   var image = Convert.ToBase64String(ms.ToArray());
   return Json(new { base64imgage = image }, JsonRequestBehavior.AllowGet);
}

HTML

 $.post(url, function (data) {
         if (data) {
               var imag = "<img "
                          + "src='" + "data:image/jpg;base64,"
                          + data.base64imgage + "'/>"; 
                 $("#divMyLetterImage").html(imag)    

} else {
             alert('Sorry, there is some error.');
         }
     }); 

Upvotes: 3

Related Questions