Is there a difference in performance between returning an image in the Controller or in the View?

Asked

Viewed 50 times

0

I just learned how to search and display an image from the database. I managed to do it two ways:

First: Through an Actionresult in my Controller:

    public ActionResult RetornaImagemDoBanco(int id)
    {
        byte[] imageData = db.Tab_Documentos_Parente.Find(id).Documento;
        return new FileStreamResult(new System.IO.MemoryStream(imageData), "image/jpeg");
    }

And in my View it’s like this:

@{ string imagem = @"/Documentos_Parente/RetornaImagemDoBanco/" + Model.Id;}
<image src=@imagem alt="" />

According to: Directly in View:

@if (Model.Documento != null)
{
   var base64 = Convert.ToBase64String(Model.Documento);
   var imgSrc = String.Format("data:image/gif;base64,{0}", base64);
   <img src = @imgSrc alt = "" />
}
else
{
   <img src = "#" alt = "" />
}

My question is: Is there any significant difference in performance between these methods? Because I’m going to return a list of student records with their respective photos and it could be hundreds of records. Suggest another way?

  • Just testing to see if there’s a difference.

  • Frankly, the two are bad, the first, by the access to database in each photo and the second by the dirt that will generate in your html, now also depends on the size of these photos in a general way. Maybe the second a little better

  • @Virgilionovic detonated me in... Kkkkk! After a lot of research, these are the only ways I found to display the bank images. But from what I said none is good. There is another way?

  • 2

    @Still, I did not detonate you, sorry if it was that impression, but, really are the ways to bring images coming from a field in your base, there is no other way not, but, the performance is low even and as you said hundreds if you can be sure that it is bad performance. I worked on a system where college students' photos were recorded the same way and had classrooms with 100 students, but then I made it work, I put a cache and went along well, so it all depends.

  • Very difficult to come to any conclusion without having the context of functionality.

  • @Virgilionovic Thanks man! I will use some type of paging to leave some 15 or less records per page (I am studying the Pagedlist.Mvc).

  • @Marcellalves the functionality is as simple as possible. A web system for registering students. Using Razor were generated the Views of Index, Edit, Details, Delete and Create. I modified the Index to display the photos of each student line by line.

  • Guys! What if I added a property to the template to receive the image? This property would not correspond to any column of the database existing only in the model (haa am using Entity Framework Database First). Then in the controller I would pass with a Foreach the images for her. Like this: foreach (student in Tab_students){ My property = Filestreamresult(new System.IO.Memorystream(imagedata), "image/jpeg");} , and I already pass the loaded model to the view. But then... what kind of data would that property?

  • 1

    The best scenario with images is to save them in a folder or in a Fileserver, and in the database save only the access path, this is a better performance scenario, if not the best practice.

Show 4 more comments
No answers

Browser other questions tagged

You are not signed in. Login or sign up in order to post.