我需要帮助在ASP .NET MVC4中通过JSON创建一组图像。
有可能吗?
我的工作代码如下,我不知道如何集成该功能。
感谢您的帮助!
[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.');
}
});
答案 0 :(得分:2)
解决方案在http://www.codeproject.com/Articles/201767/Load-Base64-Images-using-jQuery-and-MVC
所以在我的情况下,它会像
[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.');
}
});