I'm trying to display an image that is stored in the database. When the Photo field (of Image type) of the table is null, I display a default image that is stored in the directory. The problem is that the images coming from the database are not being displayed.
TheActionyouareexhibitingbydoingthetreatmentisbelow:
[HttpGet]publicFileContentResultShowFoto(int?id){byte[]fileBytes=null;stringfileType=null;try{varterapeuta=db.TERAPEUTAs.Find(id);//BuscaaimagemnoBancoif(terapeuta!=null){if(terapeuta.FOTO!=null){fileBytes=terapeuta.FOTO;//dotipobyte[]fileType=System.Drawing.Imaging.ImageFormat.Jpeg.ToString();//TipodaFotoreturnFile(fileBytes,fileType);}else{Imageimg=System.Drawing.Image.FromFile(System.Web.HttpContext.Current.Server.MapPath("~/Content/img/SemFoto.jpg"));
return File(imageToByteArray(img), System.Drawing.Imaging.ImageFormat.Jpeg.ToString());
}
}
else
{
Image img = System.Drawing.Image.FromFile(System.Web.HttpContext.Current.Server.MapPath("~/Content/img/SemFoto.jpg"));
return File(imageToByteArray(img), System.Drawing.Imaging.ImageFormat.Jpeg.ToString());
}
}
catch(Exception ex)
{
throw ex;
}
}
View's code for image display is below:
@using (Html.BeginForm("Buscar", "Terapeutas", FormMethod.Post, htmlAttributes: new { @id = "form1", @name = "form1" }))
{
@Html.ValidationSummary(true)
foreach (var item in Model.Terapeutas)
{
<img src="/Terapeutas/[email protected]_TERAPEUTA" class="img-circle" alt="Foto Terapeuta" width="120" height="120" /> }
}
}