I am very new to asp.net MVC. For last two days, I am trying to show an Image in cshtml
View from my SQL database.
Here is the method for getting the image from database:
public class ProfilePicture:Gateway
{
public byte[] ShowProfilePicture()
{
Query = "SELECT * FROM t_ProfilePicture WHERE UserId='" + 1 + "'";
Command = new SqlCommand(Query, Connection);
Connection.Open();
Reader = Command.ExecuteReader();
byte[] image = null;
while (Reader.Read())
{
image = (byte[])Reader["ProfilePictureFile"];
}
Reader.Close();
Connection.Close();
return image;
}
}
This is the HTML helper code for getting the byte data & convert it to base64:
@{
ProfilePicture aProfilePicture = new ProfilePicture();
var image = aProfilePicture.ShowProfilePicture();
string string64 = Convert.ToBase64String(image);
ViewBag.profilePicture = "data:image/png;base64," + string64;
}
And here is the <img>
tag:
<img id="profilePicture" src="@ViewBag.profilePicture" alt="profilePicture" />
I almost follow all the available solutions including this. But didn't get any solution yet.
Thank's in advance for any help.