Display Bitmap in Image Control
You can simply use something like following
<img src="@String.Format("data:image/png;base64,{0}", Convert.ToBase64String(Model.imageBytes))" />
You need to have a controller action which returns a FileStreamResult and then use an <img />
tag pointing to this action.
Action
public ActionResult Image()
{
var bitmap = GetBitmap(); // The method that returns Bitmap
var bitmapBytes = BitmapToBytes(bitmap); //Convert bitmap into a byte array
return File(bitmapBytes, "image/jpeg"); //Return as file result
}
// This method is for converting bitmap into a byte array
private static byte[] BitmapToBytes(Bitmap img)
{
using (MemoryStream stream = new MemoryStream())
{
img.Save(stream, System.Drawing.Imaging.ImageFormat.Png);
return stream.ToArray();
}
}
View
<img src='@Url.Action("image")' alt="" />