Display binary byte[] array instead of base64

kelvin nyota 21 Reputation points
2021-09-30T15:48:04.347+00:00

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;

    Httpcontext.Current.Response.BinaryWrite(buffer);

    return Httpcontext.Current.Response.OutputStream;
}
ASP.NET
ASP.NET
A set of technologies in the .NET Framework for building web applications and XML web services.
3,243 questions
0 comments No comments
{count} votes

Accepted answer
  1. AgaveJoe 26,181 Reputation points
    2021-09-30T18:40:24.627+00:00

    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";  
    }  
      
    <h2>Index</h2>  
      
    <div>  
        <img src="/File/GetTele" alt="Telecaster" />  
    </div>  
    
    0 comments No comments

3 additional answers

Sort by: Most helpful
  1. Bruce (SqlWork.com) 54,866 Reputation points
    2021-09-30T20:08:22.47+00:00

    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,061 Reputation points
    2021-10-01T05:58:25.647+00:00

    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:
    https://forums.asp.net/t/1937602.aspx?Display+Image+from+byte+received+from+DB

    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
    2021-10-01T08:36:04.947+00:00

    Bruce-SqlWork

    True

    Yijing Sun

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

    AgaveJoe

    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.