1

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.');
         }
     }); 
11
  • What does your data have? Commented Apr 30, 2013 at 18:10
  • @PSCoder It is a List<Bitmap> Commented Apr 30, 2013 at 18:11
  • 1
    Check this worldwidewhat.net/2012/07/how-to-draw-bitmaps-using-javascript Commented Apr 30, 2013 at 18:13
  • 1
    Pretty much, yes. You could also convert the bitmap list to base64 string list and return this in json format and then build up the image tags using jQuery. To convert the Bitmaps to base64 strings refer to stackoverflow.com/questions/7350679/… and then simply string base64String = Convert.ToBase64String(imageBytes);. Commented Apr 30, 2013 at 18:28
  • 1
    That's what i was talking about. You can simply adapt that and do it with a list of images. Glad you found the resource you needed. ;-) Good luck with your project. Commented Apr 30, 2013 at 18:45

1 Answer 1

3

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.');
         }
     }); 
Sign up to request clarification or add additional context in comments.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.