3

Possuo uma imagem em um array de bytes, e preciso inseri-la na view.

Há alguma maneira de eu fazer isto através de ViewBag?

1 Resposta 1

4

Sim, várias.

A que eu acho mais interessante é converter este array de bytes numa string em Base64.

var byteArray = GerarByteArray();
string strBase64 = Convert.ToBase64String(byteArray);

var imgSrc = $"data:image/png;base64,{strBase64}";

ViewBag.Base64Image = imgSrc;

Na view, você usaria algo como

<img src="@ViewBag.Base64Image />

Veja funcionando no .NET Fiddle.


Demonstração de imagem em Base64.

<img src="data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAATwAAAE8CAYAAABdBQ0GAAAAGXRFWHRTb2Z0d2FyZQBBZG9iZSBJbWFnZVJlYWR5ccllPAAAAyNpVFh0WE1MOmNvbS5hZG9iZS54bXAAAAAAADw/eHBhY2tldCBiZWdpbj0i77u/IiBpZD0iVzVNME1wQ2VoaUh6cmVTek5UY3prYzlkIj8+IDx4OnhtcG1ldGEgeG1sbnM6eD0iYWRvYmU6bnM6bWV0YS8iIHg6eG1wdGs9IkFkb2JlIFhNUCBDb3JlIDUuNi1jMDE0IDc5LjE1Njc5NywgMjAxNC8wOC8yMC0wOTo1MzowMiAgICAgICAgIj4gPHJkZjpSREYgeG1sbnM6cmRmPSJodHRwOi8vd3d3LnczLm9yZy8xOTk5LzAyLzIyLXJkZi1zeW50YXgtbnMjIj4gPHJkZjpEZXNjcmlwdGlvbiByZGY6YWJvdXQ9IiIgeG1sbnM6eG1wTU09Imh0dHA6Ly9ucy5hZG9iZS5jb20veGFwLzEuMC9tbS8iIHhtbG5zOnN0UmVmPSJodHRwOi8vbnMuYWRvYmUuY29tL3hhcC8xLjAvc1R5cGUvUmVzb3VyY2VSZWYjIiB4bWxuczp4bXA9Imh0dHA6Ly9ucy5hZG9iZS5jb20veGFwLzEuMC8iIHhtcE1NOkRvY3VtZW50SUQ9InhtcC5kaWQ6RkE1MTZBNzU1MDE1MTFFNUEzODE5OTE2QTE2M0ExOTYiIHhtcE1NOkluc3RhbmNlSUQ9Inht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QHAkGNZSg+49JJLrDsBjPAAgPAAgPAAgPAAgPAAgPAAEB4AEB4AEB4AEB4AEB4AEB4AEB4AEB4AEB4AEB4APMose6QCMMIDAMIDAMIDAMIDAMIDAMIDAMIDAMIDAMIDAMIDQHgAQHgAQHgAQHgAQHgAQHgAQHgAQHgAQHgAQHgACA8ACA8ACA8ACA8ACA8ACA8ACA8ACA8ACA8ACA8A4QEA4QEA4QEA4QEA4QEA4QEA4QEA4QEA4QEA4QEA4QEgPAAgPAAgPAAgPAAgPAAgPAAgPAAgPAAgPAAgPACEBwCEBwCEBwCEBwCEBwCEBwCEBwCEBwCEBwCEB4DwAGA0+H8BBgC+uYmSDNLUEgAAAABJRU5ErkJggg==">

3
  • "var imgSrc = $"data:image/png;base64,{base64 }";", esta parte seria para que?
    – Brayan
    Commented 10/04/2017 às 16:35
  • A tag img precisa deste prefixo (data:image/png;base64,) para "saber" que a imagem é uma string em Base64 e não um caminho. O código serve para concatenar a string em base64 junto com este prefixo.
    – Jéf Bueno
    Commented 10/04/2017 às 16:52
  • ah sim, entendi, muito obrigado, funcionou :D
    – Brayan
    Commented 10/04/2017 às 16:56

Você deve fazer log-in para responder a esta pergunta.

Esta não é a resposta que você está procurando? Pesquise outras perguntas com a tag .