Display binary byte[] array instead of base64

kelvin nyota 21 Reputation points

Am trying to display byte[] array images in asp mvc c#. But there is only base64 conversion option available. I have learnt ASP MVC 5 since 2019, now am experienced and don't want to waste CPU resources on image conversation. I am displaying an image list on my view, but don't want to convert them into base64. SQL Server does not support saving in that format. So, here is my implementation, but it is filling the whole page to load one image, and not loading all the other images.

Here is the code:

Public stream GetBinaryImage(byte[] buffer, string ContentType)

    Httpcontext.Current.Response.ContentType = ContentType;


    return Httpcontext.Current.Response.OutputStream;
A Microsoft web application framework that implements the model-view-controller (MVC) design pattern.
852 questions
0 comments No comments
{count} votes

Accepted answer
  1. AgaveJoe 22,631 Reputation points

    I assume the image in a byte[] array already. The rest is simple, return a FileResult from your MVC action and take advantage of the Controller.File() method as illustrated below

        public class FileController : Controller  
            public ActionResult Index()  
                return View();  
            // GET: GetTele  
            public FileResult GetTele()  
                byte[] fileBuffer = System.IO.File.ReadAllBytes(Server.MapPath("~/images/tele.jpg"));  
                return File(fileBuffer, "image/jpg");  

    The Index view that displays the image.

        ViewBag.Title = "Index";  
        <img src="/File/GetTele" alt="Telecaster" />  
    0 comments No comments

3 additional answers

Sort by: Most helpful
  1. Bruce (SqlWork.com) 36,181 Reputation points

    you are facing a browser limitation.

    each request can return one html page or one image. dataurls allow including the small image data (as it doubles the download size) in the html. I assume this is what you are using. dataurl used for images only support base64.

  2. Yijing Sun-MSFT 7,031 Reputation points

    Hi @kelvin nyota ,
    As far as I think,you could return byteArray in your controller.Just like this:

    return new FileContentResult(byteArray, "image/jpeg");  

    More details,you could refer to below article:

    Best regards,
    Yijing Sun

    If the answer is helpful, please click "Accept Answer" and upvote it.

    Note: Please follow the steps in our  documentation  to enable e-mail notifications if you want to receive the related email notification for this thread.

    0 comments No comments

  3. kelvin nyota 21 Reputation points



    Yijing Sun

    I have tried to return a FileContentResult before but it refused to work.


    Ok. I understand. I have used FileResult in the following scenario, but it is not sending any data to the FileResult method.

    Only int parameters and strings but objects are not supported.